Module: Dynamoid::Document::ClassMethods

Defined in:
lib/dynamoid/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abstract_class ⇒ Boolean?

Set whether this class is an abstract base class.

Abstract classes do not store Single Table Inheritance (STI) type values or scope queries by type.

class ApplicationModel
include Dynamoid::Document
self.abstract_class = true
end

Returns:

  • (Boolean, nil)


208
209
210
# File 'lib/dynamoid/document.rb', line 208

def abstract_class
  @abstract_class
end

Instance Method Details

#abstract_class? ⇒ Boolean

Returns true if the class is marked as an abstract class.

Returns:

  • (Boolean)


213
214
215
# File 'lib/dynamoid/document.rb', line 213

def abstract_class?
  defined?(@abstract_class) && @abstract_class == true
end

#attr_readonly(*read_only_attributes) ⇒ Object



44
45
46
# File 'lib/dynamoid/document.rb', line 44

def attr_readonly(*read_only_attributes)
  self.read_only_attributes.concat read_only_attributes.map(&:to_s)
end

#build(attrs = {}, &block) ⇒ Dynamoid::Document

Initialize a new object.

User.build(name: 'A')

Initialize an object and pass it into a block to set other attributes.

User.build(name: 'A') do |u|
  u.age = 21
end

The only difference between build and new methods is that build supports STI (Single table inheritance) and looks at the inheritance field. So it can build a model of actual class. For instance:

class Employee
  include Dynamoid::Document

  field :type
  field :name
end

class Manager < Employee
end

Employee.build(name: 'Alice', type: 'Manager') # => #<Manager:0x00007f945756e3f0 ...>

Parameters:

  • attrs (Hash) (defaults to: {}) —

    Attributes with which to create the document

  • block (Proc) —

    Block to process a document after initialization

Returns:

Since:

  • 0.2.0



147
148
149
# File 'lib/dynamoid/document.rb', line 147

def build(attrs = {}, &block)
  choose_right_class(attrs).new(attrs, &block)
end

#capacity_mode ⇒ Symbol

Returns the billing (capacity) mode for this table.

Could be either provisioned or on_demand.

Returns:

  • (Symbol)


69
70
71
# File 'lib/dynamoid/document.rb', line 69

def capacity_mode
  options[:capacity_mode] || Dynamoid::Config.capacity_mode
end

#count ⇒ Integer

Return the count of items for this class.

It returns approximate value based on DynamoDB statistic. DynamoDB updates it periodically so the value can be no accurate.

It's a reletively cheap operation and doesn't read all the items in a table. It makes just one HTTP request to DynamoDB.

Returns:

  • (Integer) —

    items count in a table

Since:

  • 0.6.1



109
110
111
# File 'lib/dynamoid/document.rb', line 109

def count
  Dynamoid.adapter.count(table_name)
end

#exists?(id_or_conditions = {}) ⇒ true|false

Does this model exist in a table?

User.exists?('713') # => true

If a range key is declared it should be specified in the following way:

User.exists?([['713', 'range-key-value']]) # => true

It's possible to check existence of several models at once:

User.exists?(['713', '714', '715'])

Or in case when a range key is declared:

User.exists?(
  [
    ['713', 'range-key-value-1'],
    ['714', 'range-key-value-2'],
    ['715', 'range-key-value-3']
  ]
)

It's also possible to specify models not with primary key but with conditions on the attributes (in the where method style):

User.exists?(age: 20, 'created_at.gt': Time.now - 1.day)

Parameters:

  • id_or_conditions (String|Array[String]|Array[Array]|Hash) (defaults to: {}) —

    the primary id of the model, a list of primary ids or a hash with the options to filter from.

Returns:

  • (true|false)

Since:

  • 0.2.0



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dynamoid/document.rb', line 183

def exists?(id_or_conditions = {})
  case id_or_conditions
  when Hash then where(id_or_conditions).count >= 1
  else
    begin
      find(id_or_conditions)
      true
    rescue Dynamoid::Errors::RecordNotFound
      false
    end
  end
end

#hash_key ⇒ Symbol

Returns the hash key field name for this class.

By default id field is used. But it can be overriden in the table method call.

User.hash_key # => :id

Returns:

  • (Symbol) —

    a hash key name

Since:

  • 0.4.0



95
96
97
# File 'lib/dynamoid/document.rb', line 95

def hash_key
  options[:key] || :id
end

#inheritance_field ⇒ Symbol

Returns the field name used to store the Single Table Inheritance (STI) type attribute for this table.

Default field name is type but it can be overridden in the table method call.

User.inheritance_field # => :type

Returns:

  • (Symbol) —

    name of the inheritance field



82
83
84
# File 'lib/dynamoid/document.rb', line 82

def inheritance_field
  options[:inheritance_field] || :type
end

#read_capacity ⇒ Integer

Returns the read capacity for this table.

Returns:

  • (Integer) —

    read capacity units

Since:

  • 0.4.0



52
53
54
# File 'lib/dynamoid/document.rb', line 52

def read_capacity
  options[:read_capacity] || Dynamoid::Config.read_capacity
end

#sti_class_for(type_name) ⇒ Class

Resolves a Single Table Inheritance (STI) type string into the corresponding model class.

Override this method in the base class to provide custom class mapping or to resolve renamed or legacy type strings.

Parameters:

  • type_name (String) —

    type value stored in DynamoDB

Returns:

  • (Class) —

    the resolved Dynamoid model class

Raises:



243
244
245
246
247
# File 'lib/dynamoid/document.rb', line 243

def sti_class_for(type_name)
  type_name.constantize
rescue NameError
  raise Errors::SubclassNotFound, "STI subclass does not found. Subclass: '#{type_name}'"
end

#sti_name ⇒ String

Returns the string identifier stored in the Single Table Inheritance (STI) type attribute.

Defaults to the class name. Override this method in subclasses to decouple the stored type value from the Ruby class name.

class Car < Vehicle
def self.sti_name
  'sedan'
end
end

Returns:

  • (String)


230
231
232
# File 'lib/dynamoid/document.rb', line 230

def sti_name
  name
end

#write_capacity ⇒ Integer

Returns the write_capacity for this table.

Returns:

  • (Integer) —

    write capacity units

Since:

  • 0.4.0



60
61
62
# File 'lib/dynamoid/document.rb', line 60

def write_capacity
  options[:write_capacity] || Dynamoid::Config.write_capacity
end