Module: Dynamoid::Document::ClassMethods
- Defined in:
- lib/dynamoid/document.rb
Instance Method Summary collapse
- #attr_readonly(*read_only_attributes) ⇒ Object
-
#build(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
-
#count ⇒ Object
Returns the number of items for this class.
-
#create(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object and immediately save it to the database.
-
#create!(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object and immediately save it to the database.
- #deep_subclasses ⇒ Object
-
#exists?(id_or_conditions = {}) ⇒ Boolean
Does this object exist?.
-
#hash_key ⇒ Object
Returns the id field for this class.
-
#read_capacity ⇒ Object
Returns the read_capacity for this table.
-
#table(options = {}) ⇒ Object
Set up table options, including naming it whatever you want, setting the id key, and manually overriding read and write capacity.
- #update(hash_key, range_key_value = nil, attrs) ⇒ Object
- #update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Object
- #upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Object
-
#write_capacity ⇒ Object
Returns the write_capacity for this table.
Instance Method Details
#attr_readonly(*read_only_attributes) ⇒ Object
35 36 37 |
# File 'lib/dynamoid/document.rb', line 35 def attr_readonly(*read_only_attributes) self.read_only_attributes.concat read_only_attributes.map(&:to_s) end |
#build(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object.
104 105 106 |
# File 'lib/dynamoid/document.rb', line 104 def build(attrs = {}) attrs[:type] ? attrs[:type].constantize.new(attrs) : new(attrs) end |
#count ⇒ Object
Returns the number of items for this class.
63 64 65 |
# File 'lib/dynamoid/document.rb', line 63 def count Dynamoid.adapter.count(table_name) end |
#create(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object and immediately save it to the database.
74 75 76 77 78 79 80 |
# File 'lib/dynamoid/document.rb', line 74 def create(attrs = {}) if attrs.is_a?(Array) attrs.map { |attr| create(attr) } else build(attrs).tap(&:save) end end |
#create!(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object and immediately save it to the database. Raise an exception if persistence failed.
89 90 91 92 93 94 95 |
# File 'lib/dynamoid/document.rb', line 89 def create!(attrs = {}) if attrs.is_a?(Array) attrs.map { |attr| create!(attr) } else build(attrs).tap(&:save!) end end |
#deep_subclasses ⇒ Object
193 194 195 |
# File 'lib/dynamoid/document.rb', line 193 def deep_subclasses subclasses + subclasses.map(&:deep_subclasses).flatten end |
#exists?(id_or_conditions = {}) ⇒ Boolean
Does this object exist?
115 116 117 118 119 120 |
# File 'lib/dynamoid/document.rb', line 115 def exists?(id_or_conditions = {}) case id_or_conditions when Hash then where(id_or_conditions).first.present? else !! find_by_id(id_or_conditions) end end |
#hash_key ⇒ Object
Returns the id field for this class.
56 57 58 |
# File 'lib/dynamoid/document.rb', line 56 def hash_key [:key] || :id end |
#read_capacity ⇒ Object
Returns the read_capacity for this table.
42 43 44 |
# File 'lib/dynamoid/document.rb', line 42 def read_capacity [:read_capacity] || Dynamoid::Config.read_capacity end |
#table(options = {}) ⇒ Object
Set up table options, including naming it whatever you want, setting the id key, and manually overriding read and write capacity.
30 31 32 33 |
# File 'lib/dynamoid/document.rb', line 30 def table( = {}) self. = super if defined? super end |
#update(hash_key, range_key_value = nil, attrs) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dynamoid/document.rb', line 122 def update(hash_key, range_key_value=nil, attrs) if range_key.present? range_key_value = dump_field(range_key_value, attributes[self.range_key]) else range_key_value = nil end model = find(hash_key, range_key: range_key_value, consistent_read: true) model.update_attributes(attrs) model end |
#update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dynamoid/document.rb', line 134 def update_fields(hash_key_value, range_key_value=nil, attrs={}, conditions={}) optional_params = [range_key_value, attrs, conditions].compact if optional_params.first.is_a?(Hash) range_key_value = nil attrs, conditions = optional_params[0 .. 1] else range_key_value = optional_params.first attrs, conditions = optional_params[1 .. 2] end = if range_key { range_key: dump_field(range_key_value, attributes[range_key]) } else {} end (conditions[:if_exists] ||= {})[hash_key] = hash_key_value [:conditions] = conditions begin new_attrs = Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| attrs.symbolize_keys.each do |k, v| t.set k => dump_field(v, attributes[k]) end end new(new_attrs) rescue Dynamoid::Errors::ConditionalCheckFailedException end end |
#upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/dynamoid/document.rb', line 164 def upsert(hash_key_value, range_key_value=nil, attrs={}, conditions={}) optional_params = [range_key_value, attrs, conditions].compact if optional_params.first.is_a?(Hash) range_key_value = nil attrs, conditions = optional_params[0 .. 1] else range_key_value = optional_params.first attrs, conditions = optional_params[1 .. 2] end = if range_key { range_key: dump_field(range_key_value, attributes[range_key]) } else {} end [:conditions] = conditions begin new_attrs = Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| attrs.symbolize_keys.each do |k, v| t.set k => dump_field(v, attributes[k]) end end new(new_attrs) rescue Dynamoid::Errors::ConditionalCheckFailedException end end |
#write_capacity ⇒ Object
Returns the write_capacity for this table.
49 50 51 |
# File 'lib/dynamoid/document.rb', line 49 def write_capacity [:write_capacity] || Dynamoid::Config.write_capacity end |