Class: DataMapper::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Extended by:
Equalizer, Extlib::Assertions
Includes:
Extlib::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

DataObjectsAdapter, InMemoryAdapter, YamlAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

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



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

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’)

then adapter name is currently be set to is :default

Examples:

adapter.name  # => :default

Returns:

  • (Symbol)

    the adapter name



36
37
38
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 36

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



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

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



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

def resource_naming_convention
  @resource_naming_convention
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)


85
86
87
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 85

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)


141
142
143
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 141

def delete(collection)
  raise NotImplementedError, "#{self.class}#delete not implemented"
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)


103
104
105
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 103

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)


123
124
125
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 123

def update(attributes, collection)
  raise NotImplementedError, "#{self.class}#update not implemented"
end