Class: DataMapper::Adapters::InMemoryAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- Defined in:
- lib/dm-core/adapters/in_memory_adapter.rb
Overview
This is probably the simplest functional adapter possible. It simply stores and queries from a hash containing the model classes as keys, and an array of hashes. It is not persistent whatsoever; when the Ruby process finishes, everything that was stored it lost. However, it doesn’t require any other external libraries, such as data_objects, so it is ideal for writing specs against. It also serves as an excellent example for budding adapter developers, so it is critical that it remains well documented and up to date.
Instance Attribute Summary
Attributes inherited from AbstractAdapter
#field_naming_convention, #name, #options, #resource_naming_convention
Instance Method Summary collapse
-
#create(resources) ⇒ Object
Used by DataMapper to put records into a data-store: “INSERT” in SQL-speak.
-
#delete(collection) ⇒ Integer
Destroys all the records matching the given query.
-
#read(query) ⇒ Array
Looks up one record or a collection of records from the data-store: “SELECT” in SQL.
-
#reset ⇒ Object
TODO consider proper automigrate functionality.
-
#update(attributes, collection) ⇒ Object
Used by DataMapper to update the attributes on existing records in a data-store: “UPDATE” in SQL-speak.
Methods inherited from AbstractAdapter
#attributes_as_fields, descendants, inherited, #initialize_serial, #new_query
Methods included from DataMapper::Assertions
Methods included from Equalizer
Instance Method Details
#create(resources) ⇒ Object
Used by DataMapper to put records into a data-store: “INSERT” in SQL-speak. It takes an array of the resources (model instances) to be saved. Resources each have a key that can be used to quickly look them up later without searching, if the adapter supports it.
21 22 23 24 25 26 27 28 |
# File 'lib/dm-core/adapters/in_memory_adapter.rb', line 21 def create(resources) records = records_for(resources.first.model) resources.each do |resource| initialize_serial(resource, records.size.succ) records << attributes_as_fields(resource.attributes(nil)) end end |
#delete(collection) ⇒ Integer
Destroys all the records matching the given query. “DELETE” in SQL.
70 71 72 73 74 75 |
# File 'lib/dm-core/adapters/in_memory_adapter.rb', line 70 def delete(collection) records = records_for(collection.model) records_to_delete = collection.query.filter_records(records.dup) records.replace(records - records_to_delete) records_to_delete.size end |
#read(query) ⇒ Array
Looks up one record or a collection of records from the data-store: “SELECT” in SQL.
41 42 43 |
# File 'lib/dm-core/adapters/in_memory_adapter.rb', line 41 def read(query) query.filter_records(records_for(query.model).dup) end |
#reset ⇒ Object
TODO consider proper automigrate functionality
78 79 80 |
# File 'lib/dm-core/adapters/in_memory_adapter.rb', line 78 def reset @records = {} end |
#update(attributes, collection) ⇒ Object
Used by DataMapper to update the attributes on existing records in a data-store: “UPDATE” in SQL-speak. It takes a hash of the attributes to update with, as well as a collection object that specifies which resources should be updated.
56 57 58 59 |
# File 'lib/dm-core/adapters/in_memory_adapter.rb', line 56 def update(attributes, collection) attributes = attributes_as_fields(attributes) read(collection.query).each { |record| record.update(attributes) }.size end |