Class: DataMapper::Adapters::AbstractAdapter

### Home Classes Methods

### In Files

- lib/dm-core/adapters/abstract_adapter.rb

### Parent

Object

### Methods

- ::descendants
- ::inherited
- ::new
- #attributes_as_fields
- #create
- #delete
- #initialize_serial
- #new_query
- #read
- #update

### Included Modules

- DataMapper::Assertions

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Adapters::AbstractAdapter
- DataMapper::Adapters::InMemoryAdapter
- DataMapper::Assertions
- DataMapper::Associations
- DataMapper::Associations::ManyToMany
- DataMapper::Associations::ManyToMany::Collection
- DataMapper::Associations::ManyToMany::Relationship
- DataMapper::Associations::ManyToOne
- DataMapper::Associations::ManyToOne::Relationship
- DataMapper::Associations::OneToMany
- DataMapper::Associations::OneToMany::Collection
- DataMapper::Associations::OneToMany::Relationship
- DataMapper::Associations::OneToOne
- DataMapper::Associations::OneToOne::Relationship
- DataMapper::Associations::Relationship
- DataMapper::Chainable
- DataMapper::Collection
- DataMapper::Deprecate
- DataMapper::DescendantSet
- DataMapper::Equalizer
- DataMapper::Ext
- DataMapper::Ext::Array
- DataMapper::Ext::Hash
- DataMapper::Ext::Module
- DataMapper::Ext::Object
- DataMapper::Ext::String
- DataMapper::Hook
- DataMapper::Hook::ClassMethods
- DataMapper::IdentityMap
- DataMapper::ImmutableDeletedError
- DataMapper::ImmutableError
- DataMapper::IncompleteModelError
- DataMapper::Inflector
- DataMapper::Inflector::Inflections
- DataMapper::LocalObjectSpace
- DataMapper::Logger
- DataMapper::Mash
- DataMapper::Model
- DataMapper::Model::Hook
- DataMapper::Model::Hook::MethodCommand
- DataMapper::Model::Hook::Methods
- DataMapper::Model::Hook::ProcCommand
- DataMapper::Model::Is
- DataMapper::Model::Property
- DataMapper::Model::Relationship
- DataMapper::Model::Scope
- DataMapper::NamingConventions
- DataMapper::NamingConventions::Field
- DataMapper::NamingConventions::Field::Underscored
- DataMapper::NamingConventions::Field::UnderscoredAndPluralized
- DataMapper::NamingConventions::Field::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Field::Yaml
- DataMapper::NamingConventions::Resource
- DataMapper::NamingConventions::Resource::Underscored
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralized
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutLeadingModule
- DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
- DataMapper::NamingConventions::Resource::Yaml
- DataMapper::ObjectNotFoundError
- DataMapper::OrderedSet
- DataMapper::OrderedSet::Cache
- DataMapper::OrderedSet::Cache::API
- DataMapper::PersistenceError
- DataMapper::PluginNotFoundError
- DataMapper::Property
- DataMapper::Property::Binary
- DataMapper::Property::Boolean
- DataMapper::Property::Class
- DataMapper::Property::Date
- DataMapper::Property::DateTime
- DataMapper::Property::Decimal
- DataMapper::Property::Discriminator
- DataMapper::Property::Discriminator::Model
- DataMapper::Property::Float
- DataMapper::Property::Integer
- DataMapper::Property::Lookup
- DataMapper::Property::Numeric
- DataMapper::Property::Object
- DataMapper::Property::PassThroughLoadDump
- DataMapper::Property::Serial
- DataMapper::Property::String
- DataMapper::Property::Text
- DataMapper::Property::Time
- DataMapper::Property::Typecast
- DataMapper::Property::Typecast::Numeric
- DataMapper::Property::Typecast::Time
- DataMapper::PropertySet
- DataMapper::Query
- DataMapper::Query::Conditions
- DataMapper::Query::Conditions::AbstractComparison
- DataMapper::Query::Conditions::AbstractOperation
- DataMapper::Query::Conditions::AndOperation
- DataMapper::Query::Conditions::Comparison
- DataMapper::Query::Conditions::EqualToComparison
- DataMapper::Query::Conditions::FlattenOperation
- DataMapper::Query::Conditions::GreaterThanComparison
- DataMapper::Query::Conditions::GreaterThanOrEqualToComparison
- DataMapper::Query::Conditions::InclusionComparison
- DataMapper::Query::Conditions::LessThanComparison
- DataMapper::Query::Conditions::LessThanOrEqualToComparison
- DataMapper::Query::Conditions::LikeComparison
- DataMapper::Query::Conditions::NotOperation
- DataMapper::Query::Conditions::NullOperation
- DataMapper::Query::Conditions::Operation
- DataMapper::Query::Conditions::OrOperation
- DataMapper::Query::Conditions::RegexpComparison
- DataMapper::Query::Conditions::RelationshipHandler
- DataMapper::Query::Direction
- DataMapper::Query::Operator
- DataMapper::Query::Path
- DataMapper::Query::Sort
- DataMapper::RelationshipSet
- DataMapper::Repository
- DataMapper::RepositoryNotSetupError
- DataMapper::Resource
- DataMapper::Resource::PersistenceState
- DataMapper::Resource::PersistenceState::Clean
- DataMapper::Resource::PersistenceState::Deleted
- DataMapper::Resource::PersistenceState::Dirty
- DataMapper::Resource::PersistenceState::Immutable
- DataMapper::Resource::PersistenceState::Persisted
- DataMapper::Resource::PersistenceState::Transient
- DataMapper::SaveFailureError
- DataMapper::Spec
- DataMapper::Spec::Adapters
- DataMapper::Spec::Adapters::Adapter
- DataMapper::Spec::Adapters::Helpers
- DataMapper::Spec::Adapters::InMemoryAdapter
- DataMapper::Spec::CollectionHelpers
- DataMapper::Spec::CollectionHelpers::GroupMethods
- DataMapper::Spec::Helpers
- DataMapper::Spec::PendingHelpers
- DataMapper::Subject
- DataMapper::SubjectSet
- DataMapper::SubjectSet::NameCache
- DataMapper::Undefined
- DataMapper::UnknownRelationshipError
- DataMapper::UnsavedParentError
- DataMapper::UpdateConflictError
- Blog
- Blog::Article
- ChildSubType
- CounterAdapter
- Heffalump
- Kernel
- LazyArray
- Object
- OtherComparison
- Pathname
- SubType
- Symbol

No matching classes.

# 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

field_naming_convention[RW]

A callable object returning a naming convention for property fields

@example

    adapter.field_naming_convention  # => Proc for field name

@return [call]

    object to return the naming convention for each field

@api semipublic

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 [call]

    object to return the naming convention for each model

@api semipublic

### Public Class Methods

descendants() click to toggle source

@api semipublic

    # File lib/dm-core/adapters/abstract_adapter.rb, line 22
    def self.descendants
      @descendants ||= DescendantSet.new
    end

inherited(descendant) click to toggle source

@api private

    # File lib/dm-core/adapters/abstract_adapter.rb, line 27
    def self.inherited(descendant)
      descendants << descendant
    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
    def initialize(name, options)
      @name                       = name
      @options                    = options.dup.freeze
      @resource_naming_convention = NamingConventions::Resource::UnderscoredAndPluralized
      @field_naming_convention    = NamingConventions::Field::Underscored
    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<Resource>] 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
    def create(resources)
      raise NotImplementedError, "#{self.class}#create not implemented"
    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
    def delete(collection)
      raise NotImplementedError, "#{self.class}#delete not implemented"
    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
    def new_query(repository, model, options = {})
      Query.new(repository, model, options)
    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<Hash>]

    an array of hashes to become resources

@api semipublic

    # File lib/dm-core/adapters/abstract_adapter.rb, line 113
    def read(query)
      raise NotImplementedError, "#{self.class}#read not implemented"
    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
    def update(attributes, collection)
      raise NotImplementedError, "#{self.class}#update not implemented"
    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
    def attributes_as_fields(attributes)
      Hash[ attributes.map { |property, value| [ property.field, property.dump(value) ] } ]
    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
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.