Included Modules

Class Index [+]

Quicksearch

DataMapper::Adapters::AbstractAdapter

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.

Attributes

name[R]

Adapter name

@example

  adapter.name  # => :default

Note that when you use

DataMapper.setup(:default, ‘postgres://postgres@localhost/dm_core_test’)

the adapter name is currently set to :default

@return [Symbol]

  the adapter name

@api semipublic

options[R]

Options with which adapter was set up

@example

  adapter.options  # => { :adapter => 'yaml', :path => '/tmp' }

@return [Hash]

  adapter configuration options

@api semipublic

resource_naming_convention[RW]

A callable object returning a naming convention for model storage

@example

  adapter.resource_naming_convention  # => Proc for model storage name

@return [#]

  object to return the naming convention for each model

@api semipublic

field_naming_convention[RW]

A callable object returning a naming convention for property fields

@example

  adapter.field_naming_convention  # => Proc for field name

@return [#]

  object to return the naming convention for each field

@api semipublic

Public Class Methods

descendants() click to toggle source

@api semipublic

    # File lib/dm-core/adapters/abstract_adapter.rb, line 22
22:       def self.descendants
23:         @descendants ||= DescendantSet.new
24:       end
inherited(descendant) click to toggle source

@api private

    # File lib/dm-core/adapters/abstract_adapter.rb, line 27
27:       def self.inherited(descendant)
28:         descendants << descendant
29:       end
new(name, options) click to toggle source

Initialize an AbstractAdapter instance

@param [Symbol] name

  the adapter repository name

@param [Hash] options

  the adapter configuration options

@return [undefined]

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 226
226:       def initialize(name, options)
227:         @name                       = name
228:         @options                    = options.dup.freeze
229:         @resource_naming_convention = NamingConventions::Resource::UnderscoredAndPluralized
230:         @field_naming_convention    = NamingConventions::Field::Underscored
231:       end

Public Instance Methods

create(resources) click to toggle source

Persists one or many new resources

@example

  adapter.create(collection)  # => 1

Adapters provide specific implementation of this method

@param [Enumerable] resources

  The list of resources (model instances) to create

@return [Integer]

  The number of records that were actually saved into the data-store

@api semipublic

    # File lib/dm-core/adapters/abstract_adapter.rb, line 95
95:       def create(resources)
96:         raise NotImplementedError, "#{self.class}#create not implemented"
97:       end
delete(collection) click to toggle source

Deletes one or many existing resources

@example

  adapter.delete(collection)  # => 1

Adapters provide specific implementation of this method

@param [Collection] collection

  collection of records to be deleted

@return [Integer]

  the number of records deleted

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 151
151:       def delete(collection)
152:         raise NotImplementedError, "#{self.class}#delete not implemented"
153:       end
new_query(repository, model, options = {}) click to toggle source

Create a Query object or subclass.

Alter this method if you’d like to return an adapter specific Query subclass.

@param [Repository] repository

  the Repository to retrieve results from

@param [Model] model

  the Model to retrieve results from

@param [Hash] options

  the conditions and scope

@return [Query]

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 171
171:       def new_query(repository, model, options = {})
172:         Query.new(repository, model, options)
173:       end
read(query) click to toggle source

Reads one or many resources from a datastore

@example

  adapter.read(query)  # => [ { 'name' => 'Dan Kubb' } ]

Adapters provide specific implementation of this method

@param [Query] query

  the query to match resources in the datastore

@return [Enumerable]

  an array of hashes to become resources

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 113
113:       def read(query)
114:         raise NotImplementedError, "#{self.class}#read not implemented"
115:       end
update(attributes, collection) click to toggle source

Updates one or many existing resources

@example

  adapter.update(attributes, collection)  # => 1

Adapters provide specific implementation of this method

@param [Hash(Property => Object)] attributes

  hash of attribute values to set, keyed by Property

@param [Collection] collection

  collection of records to be updated

@return [Integer]

  the number of records updated

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 133
133:       def update(attributes, collection)
134:         raise NotImplementedError, "#{self.class}#update not implemented"
135:       end

Protected Instance Methods

attributes_as_fields(attributes) click to toggle source

Translate the attributes into a Hash with the field as the key

@example

  attributes = { User.properties[:name] => 'Dan Kubb' }
  adapter.attributes_as_fields(attributes)  # => { 'name' => 'Dan Kubb' }

@param [Hash] attributes

  the attributes with the Property as the key

@return [Hash]

  the attributes with the Property#field as the key

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 210
210:       def attributes_as_fields(attributes)
211:         Hash[ attributes.map { |property, value| [ property.field, property.dump(value) ] } ]
212:       end
initialize_serial(resource, next_id) click to toggle source

Set the serial value of the Resource

@param [Resource] resource

  the resource to set the serial property in

@param [Integer] next_id

  the identifier to set in the resource

@return [undefined]

@api semipublic

     # File lib/dm-core/adapters/abstract_adapter.rb, line 187
187:       def initialize_serial(resource, next_id)
188:         return unless serial = resource.model.serial(name)
189:         return unless serial.get!(resource).nil?
190:         serial.set!(resource, next_id)
191: 
192:         # TODO: replace above with this, once
193:         # specs can handle random, non-sequential ids
194:         #serial.set!(resource, rand(2**32))
195:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.