Class: DataMapper::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
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

CounterAdapter, InMemoryAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataMapper::Assertions

assert_kind_of

Methods included from Equalizer

equalize

Instance Attribute Details

#field_naming_convention#call

A callable object returning a naming convention for property fields

Examples:

adapter.field_naming_convention  # => Proc for field name

Returns:

  • (#call)

    object to return the naming convention for each field



79
80
81
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 79

def field_naming_convention
  @field_naming_convention
end

#nameSymbol (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

Examples:

adapter.name  # => :default

Returns:

  • (Symbol)

    the adapter name



46
47
48
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 46

def name
  @name
end

#optionsHash (readonly)

Options with which adapter was set up

Examples:

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

Returns:

  • (Hash)

    adapter configuration options



57
58
59
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 57

def options
  @options
end

#resource_naming_convention#call

A callable object returning a naming convention for model storage

Examples:

adapter.resource_naming_convention  # => Proc for model storage name

Returns:

  • (#call)

    object to return the naming convention for each model



68
69
70
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 68

def resource_naming_convention
  @resource_naming_convention
end

Class Method Details

.descendantsObject



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

#create(resources) ⇒ Integer

Persists one or many new resources

Adapters provide specific implementation of this method

Examples:

adapter.create(collection)  # => 1

Parameters:

  • resources (Enumerable<Resource>)

    The list of resources (model instances) to create

Returns:

  • (Integer)

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

Raises:

  • (NotImplementedError)


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

Examples:

adapter.delete(collection)  # => 1

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted

Raises:

  • (NotImplementedError)


151
152
153
# 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 = {}) ⇒ 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?

Parameters:

  • repository (Repository)

    the Repository to retrieve results from

  • model (Model)

    the Model to retrieve results from

  • options (Hash) (defaults to: {})

    the conditions and scope

Returns:



171
172
173
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 171

def new_query(repository, model, options = {})
  Query.new(repository, model, options)
end

#read(query) ⇒ Enumerable<Hash>

Reads one or many resources from a datastore

Adapters provide specific implementation of this method

Examples:

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

Parameters:

  • query (Query)

    the query to match resources in the datastore

Returns:

  • (Enumerable<Hash>)

    an array of hashes to become resources

Raises:

  • (NotImplementedError)


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

Examples:

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

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated

Raises:

  • (NotImplementedError)


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