Module: Dynamoid::Document::ClassMethods
- Defined in:
- lib/dynamoid/document.rb
Instance Method Summary collapse
- #attr_readonly(*read_only_attributes) ⇒ Object
-
#build(attrs = {}, &block) ⇒ Dynamoid::Document
Initialize a new object.
-
#capacity_mode ⇒ Symbol
Returns the billing (capacity) mode for this table.
- #choose_right_class(attrs) ⇒ Object
-
#count ⇒ Integer
Return the count of items for this class.
- #deep_subclasses ⇒ Object
-
#exists?(id_or_conditions = {}) ⇒ true|false
Does this model exist in a table?.
-
#hash_key ⇒ Symbol
Returns the hash key field name for this class.
-
#inheritance_field ⇒ Object
Returns the field name used to support STI for this table.
-
#read_capacity ⇒ Integer
Returns the read capacity for this table.
- #table(options = {}) ⇒ Object
-
#write_capacity ⇒ Integer
Returns the write_capacity for this table.
Instance Method Details
#attr_readonly(*read_only_attributes) ⇒ Object
26 27 28 |
# File 'lib/dynamoid/document.rb', line 26 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 ...>
122 123 124 |
# File 'lib/dynamoid/document.rb', line 122 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
.
51 52 53 |
# File 'lib/dynamoid/document.rb', line 51 def capacity_mode [:capacity_mode] || Dynamoid::Config.capacity_mode end |
#choose_right_class(attrs) ⇒ Object
175 176 177 |
# File 'lib/dynamoid/document.rb', line 175 def choose_right_class(attrs) attrs[inheritance_field] ? attrs[inheritance_field].constantize : self end |
#count ⇒ Integer
Return the count of items for this class.
It returns aproximate value based on DynamoDB statistic. DynamoDB updates it periodicaly so the value can be no accurate.
It’s a reletivly cheap operation and doesn’t read all the items in a table. It makes just one HTTP request to DynamoDB.
88 89 90 |
# File 'lib/dynamoid/document.rb', line 88 def count Dynamoid.adapter.count(table_name) end |
#deep_subclasses ⇒ Object
170 171 172 |
# File 'lib/dynamoid/document.rb', line 170 def deep_subclasses subclasses + subclasses.map(&:deep_subclasses).flatten 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)
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dynamoid/document.rb', line 156 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
74 75 76 |
# File 'lib/dynamoid/document.rb', line 74 def hash_key [:key] || :id end |
#inheritance_field ⇒ Object
Returns the field name used to support STI for this table.
Default field name is type
but it can be overrided in the table
method call.
User.inheritance_field # => :type
61 62 63 |
# File 'lib/dynamoid/document.rb', line 61 def inheritance_field [:inheritance_field] || :type end |
#read_capacity ⇒ Integer
Returns the read capacity for this table.
34 35 36 |
# File 'lib/dynamoid/document.rb', line 34 def read_capacity [:read_capacity] || Dynamoid::Config.read_capacity end |
#table(options = {}) ⇒ Object
21 22 23 24 |
# File 'lib/dynamoid/document.rb', line 21 def table( = {}) self. = super if defined? super end |