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
79 80 81 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 79 def field_naming_convention @field_naming_convention end |
#name ⇒ Symbol (readonly)
Adapter name
Note that when you use
DataMapper.setup(:default, ‘postgres://postgres@localhost/dm_core_test’)
the adapter name is currently set to :default
46 47 48 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 46 def name @name end |
#options ⇒ Hash (readonly)
Options with which adapter was set up
57 58 59 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 57 def @options end |
#resource_naming_convention ⇒ #call
A callable object returning a naming convention for model storage
68 69 70 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 68 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 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 27 def self.inherited(descendant) descendants << descendant end |
Instance Method Details
#attributes_as_fields(attributes) ⇒ Hash (protected)
Translate the attributes into a Hash with the field as the key
210 211 212 213 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 210 def attributes_as_fields(attributes) pairs = attributes.map { |property, value| [ property.field, property.dump(value) ] } DataMapper::Ext::Array.to_hash(pairs) end |
#create(resources) ⇒ Integer
Persists one or many new resources
Adapters provide specific implementation of this method
95 96 97 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 95 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
151 152 153 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 151 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
187 188 189 190 191 192 193 194 195 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 187 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?
171 172 173 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 171 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
113 114 115 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 113 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
133 134 135 |
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 133 def update(attributes, collection) raise NotImplementedError, "#{self.class}#update not implemented" end |