Class: DataMapper::Adapters::AbstractAdapter
- Inherits:
-
Object
- Object
- DataMapper::Adapters::AbstractAdapter
- Extended by:
- DataMapper::Assertions, Equalizer
- Includes:
- DataMapper::Assertions
- Defined in:
- lib/dm-core/adapters/abstract_adapter.rb
Overview
Specific adapters extend this class and implement methods for creating, reading, updating and deleting records.
Adapters may only implement method for reading or (less common case) writing. Read only adapter may be useful when one needs to work with legacy data that should not be changed or web services that only provide read access to data (from Wordnet and Medline to Atom and RSS syndication feeds)
Note that in case of adapters to relational databases it makes sense to inherit from DataObjectsAdapter class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#field_naming_convention ⇒ #call
A callable object returning a naming convention for property fields.
-
#name ⇒ Symbol
readonly
Adapter name.
-
#options ⇒ Hash
readonly
Options with which adapter was set up.
-
#resource_naming_convention ⇒ #call
A callable object returning a naming convention for model storage.
Class Method Summary collapse
Instance Method Summary collapse
-
#attributes_as_fields(attributes) ⇒ Hash
protected
Translate the attributes into a Hash with the field as the key.
-
#create(resources) ⇒ Integer
Persists one or many new resources.
-
#delete(collection) ⇒ Integer
Deletes one or many existing resources.
-
#initialize_serial(resource, next_id) ⇒ undefined
protected
Set the serial value of the Resource.
-
#new_query(repository, model, options = {}) ⇒ Query
Create a Query object or subclass.
-
#read(query) ⇒ Enumerable<Hash>
Reads one or many resources from a datastore.
-
#update(attributes, collection) ⇒ Integer
Updates one or many existing resources.
Methods included from DataMapper::Assertions
Methods included from Equalizer
Instance Attribute Details
#field_naming_convention ⇒ #call
A callable object returning a naming convention for property fields
80 81 82 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 80 def field_naming_convention @field_naming_convention end |
#name ⇒ Symbol (readonly)
Adapter name
Note that when you use
DataMapper.setup(:default, 'postgres://[email protected]/dm_core_test')
the adapter name is currently set to :default
47 48 49 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 47 def name @name end |
#options ⇒ Hash (readonly)
Options with which adapter was set up
58 59 60 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 58 def @options end |
#resource_naming_convention ⇒ #call
A callable object returning a naming convention for model storage
69 70 71 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 69 def resource_naming_convention @resource_naming_convention end |
Class Method Details
.descendants ⇒ Object
22 23 24 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 22 def self.descendants @descendants ||= DescendantSet.new end |
.inherited(descendant) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
27 28 29 30 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 27 def self.inherited(descendant) descendants << descendant super end |
Instance Method Details
#attributes_as_fields(attributes) ⇒ Hash (protected)
Translate the attributes into a Hash with the field as the key
211 212 213 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 211 def attributes_as_fields(attributes) Hash[ attributes.map { |property, value| [ property.field, property.dump(value) ] } ] end |
#create(resources) ⇒ Integer
Persists one or many new resources
Adapters provide specific implementation of this method
96 97 98 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 96 def create(resources) raise NotImplementedError, "#{self.class}#create not implemented" end |
#delete(collection) ⇒ Integer
Deletes one or many existing resources
Adapters provide specific implementation of this method
152 153 154 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 152 def delete(collection) raise NotImplementedError, "#{self.class}#delete not implemented" end |
#initialize_serial(resource, next_id) ⇒ undefined (protected)
Set the serial value of the Resource
188 189 190 191 192 193 194 195 196 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 188 def initialize_serial(resource, next_id) return unless serial = resource.model.serial(name) return unless serial.get!(resource).nil? serial.set!(resource, next_id) # TODO: replace above with this, once # specs can handle random, non-sequential ids #serial.set!(resource, rand(2**32)) end |
#new_query(repository, model, options = {}) ⇒ Query
Create a Query object or subclass.
Alter this method if you'd like to return an adapter specific Query subclass.
– TODO: DataObjects::Connection.create_command style magic (Adapter)::Query?
172 173 174 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 172 def new_query(repository, model, = {}) Query.new(repository, model, ) end |
#read(query) ⇒ Enumerable<Hash>
Reads one or many resources from a datastore
Adapters provide specific implementation of this method
114 115 116 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 114 def read(query) raise NotImplementedError, "#{self.class}#read not implemented" end |
#update(attributes, collection) ⇒ Integer
Updates one or many existing resources
Adapters provide specific implementation of this method
134 135 136 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 134 def update(attributes, collection) raise NotImplementedError, "#{self.class}#update not implemented" end |