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. 
- #choose_right_class(attrs) ⇒ 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. 
- #inc(hash_key_value, range_key_value = nil, counters) ⇒ Object
- 
  
    
      #inheritance_field  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the field name used to support STI for this table. 
- 
  
    
      #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)  ⇒ Dynamoid::Doument 
    
    
  
  
  
  
  
  
  
  
  
    Update document with provided values. 
- 
  
    
      #update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions = {})  ⇒ Dynamoid::Document/nil 
    
    
  
  
  
  
  
  
  
  
  
    Update document. 
- 
  
    
      #upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {})  ⇒ Dynamoid::Document/nil 
    
    
  
  
  
  
  
  
  
  
  
    Update existing document or create new one. 
- 
  
    
      #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.
| 109 110 111 | # File 'lib/dynamoid/document.rb', line 109 def build(attrs = {}) choose_right_class(attrs).new(attrs) end | 
#choose_right_class(attrs) ⇒ Object
| 293 294 295 | # File 'lib/dynamoid/document.rb', line 293 def choose_right_class(attrs) attrs[inheritance_field] ? attrs[inheritance_field].constantize : self end | 
#count ⇒ Object
Returns the number of items for this class.
| 68 69 70 | # File 'lib/dynamoid/document.rb', line 68 def count Dynamoid.adapter.count(table_name) end | 
#create(attrs = {}) ⇒ Dynamoid::Document
Initialize a new object and immediately save it to the database.
| 79 80 81 82 83 84 85 | # File 'lib/dynamoid/document.rb', line 79 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.
| 94 95 96 97 98 99 100 | # File 'lib/dynamoid/document.rb', line 94 def create!(attrs = {}) if attrs.is_a?(Array) attrs.map { |attr| create!(attr) } else build(attrs).tap(&:save!) end end | 
#deep_subclasses ⇒ Object
| 289 290 291 | # File 'lib/dynamoid/document.rb', line 289 def deep_subclasses subclasses + subclasses.map(&:deep_subclasses).flatten end | 
#exists?(id_or_conditions = {}) ⇒ Boolean
Does this object exist?
| 120 121 122 123 124 125 126 127 128 129 130 131 | # File 'lib/dynamoid/document.rb', line 120 def exists?(id_or_conditions = {}) case id_or_conditions when Hash then where(id_or_conditions).first.present? else begin find(id_or_conditions) true rescue Dynamoid::Errors::RecordNotFound false end end end | 
#hash_key ⇒ Object
Returns the id field for this class.
| 61 62 63 | # File 'lib/dynamoid/document.rb', line 61 def hash_key [:key] || :id end | 
#inc(hash_key_value, range_key_value = nil, counters) ⇒ Object
| 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | # File 'lib/dynamoid/document.rb', line 270 def inc(hash_key_value, range_key_value=nil, counters) = if range_key value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key]) value_dumped = Dumping.dump_field(value_casted, attributes[range_key]) { range_key: value_dumped } else {} end Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| counters.each do |k, v| value_casted = TypeCasting.cast_field(v, attributes[k]) value_dumped = Dumping.dump_field(value_casted, attributes[k]) t.add(k => value_dumped) end end end | 
#inheritance_field ⇒ Object
Returns the field name used to support STI for this table.
| 54 55 56 | # File 'lib/dynamoid/document.rb', line 54 def inheritance_field [:inheritance_field] || :type 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) ⇒ Dynamoid::Doument
Update document with provided values. Instantiates document and saves changes. Runs validations and callbacks.
| 144 145 146 147 148 | # File 'lib/dynamoid/document.rb', line 144 def update(hash_key, range_key_value = nil, attrs) 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 = {}) ⇒ Dynamoid::Document/nil
Update document. Uses efficient low-level ‘UpdateItem` API call. Changes attibutes and loads new document version with one API call. Doesn’t run validations and callbacks. Can make conditional update. If a document doesn’t exist or specified conditions failed - returns ‘nil`
| 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | # File 'lib/dynamoid/document.rb', line 168 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 value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key]) value_dumped = Dumping.dump_field(value_casted, attributes[range_key]) { range_key: value_dumped } else {} end (conditions[:if_exists] ||= {})[hash_key] = hash_key_value [:conditions] = conditions attrs = attrs.symbolize_keys if Dynamoid::Config. attrs[:updated_at] ||= DateTime.now.in_time_zone(Time.zone) end begin new_attrs = Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| attrs.each do |k, v| value_casted = TypeCasting.cast_field(v, attributes[k]) value_dumped = Dumping.dump_field(value_casted, attributes[k]) t.set(k => value_dumped) end end attrs_undumped = Undumping.undump_attributes(new_attrs, attributes) new(attrs_undumped) rescue Dynamoid::Errors::ConditionalCheckFailedException end end | 
#upsert(hash_key_value, range_key_value = nil, attrs = {}, conditions = {}) ⇒ Dynamoid::Document/nil
Update existing document or create new one. Similar to ‘.update_fields`. The only diffirence is creating new document.
Uses efficient low-level ‘UpdateItem` API call. Changes attibutes and loads new document version with one API call. Doesn’t run validations and callbacks. Can make conditional update. If specified conditions failed - returns ‘nil`
| 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | # File 'lib/dynamoid/document.rb', line 229 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 value_casted = TypeCasting.cast_field(range_key_value, attributes[range_key]) value_dumped = Dumping.dump_field(value_casted, attributes[range_key]) { range_key: value_dumped } else {} end [:conditions] = conditions attrs = attrs.symbolize_keys if Dynamoid::Config. attrs[:updated_at] ||= DateTime.now.in_time_zone(Time.zone) end begin new_attrs = Dynamoid.adapter.update_item(table_name, hash_key_value, ) do |t| attrs.each do |k, v| value_casted = TypeCasting.cast_field(v, attributes[k]) value_dumped = Dumping.dump_field(value_casted, attributes[k]) t.set(k => value_dumped) end end attrs_undumped = Undumping.undump_attributes(new_attrs, attributes) new(attrs_undumped) rescue Dynamoid::Errors::ConditionalCheckFailedException end end |