Module: Superstore::Persistence::ClassMethods
- Defined in:
- lib/superstore/persistence.rb
Instance Method Summary collapse
- #batch(&block) ⇒ Object
- #batching? ⇒ Boolean
- #delete(ids) ⇒ Object
- #delete_all ⇒ Object
- #encode_attributes(attributes) ⇒ Object
- #insert_record(id, attributes) ⇒ Object
- #instantiate(id, attributes) ⇒ Object
- #update_record(id, attributes) ⇒ Object
Instance Method Details
#batch(&block) ⇒ Object
30 31 32 |
# File 'lib/superstore/persistence.rb', line 30 def batch(&block) adapter.batch(&block) end |
#batching? ⇒ Boolean
26 27 28 |
# File 'lib/superstore/persistence.rb', line 26 def batching? adapter.batching? end |
#delete(ids) ⇒ Object
10 11 12 |
# File 'lib/superstore/persistence.rb', line 10 def delete(ids) adapter.delete table_name, ids end |
#delete_all ⇒ Object
14 15 16 |
# File 'lib/superstore/persistence.rb', line 14 def delete_all adapter.execute "TRUNCATE #{table_name}" end |
#encode_attributes(attributes) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/superstore/persistence.rb', line 44 def encode_attributes(attributes) encoded = {} attributes.each do |column_name, value| if value.nil? encoded[column_name] = nil else encoded[column_name] = attribute_definitions[column_name].type.encode(value) end end encoded end |
#insert_record(id, attributes) ⇒ Object
18 19 20 |
# File 'lib/superstore/persistence.rb', line 18 def insert_record(id, attributes) adapter.insert table_name, id, encode_attributes(attributes) end |
#instantiate(id, attributes) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/superstore/persistence.rb', line 34 def instantiate(id, attributes) allocate.tap do |object| object.instance_variable_set("@id", id) if id object.instance_variable_set("@new_record", false) object.instance_variable_set("@destroyed", false) object.instance_variable_set("@attributes", typecast_persisted_attributes(attributes)) object.instance_variable_set("@association_cache", {}) end end |
#update_record(id, attributes) ⇒ Object
22 23 24 |
# File 'lib/superstore/persistence.rb', line 22 def update_record(id, attributes) adapter.update table_name, id, encode_attributes(attributes) end |