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 Equalizer

equalize

Methods included from DataMapper::Assertions

assert_kind_of

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


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

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


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

def name
  @name
end

#optionsHash (readonly)

Options with which adapter was set up

Examples:

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


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

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


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

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
30
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 27

def self.inherited(descendant)
  descendants << descendant
  super
end

Instance Method Details

#attributes_as_fields(attributes) ⇒ Hash (protected)

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

Examples:

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


211
212
213
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 211

def attributes_as_fields(attributes)
  Hash[ attributes.map { |property, value| [ property.field, property.dump(value) ] } ]
end

#create(resources) ⇒ Integer

Persists one or many new resources

Adapters provide specific implementation of this method

Examples:

adapter.create(collection)  # => 1

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 96

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

Raises:

  • (NotImplementedError)


152
153
154
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 152

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



188
189
190
191
192
193
194
195
196
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 188

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?



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

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' } ]

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 114

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

Raises:

  • (NotImplementedError)


134
135
136
# File 'lib/dm-core/adapters/abstract_adapter.rb', line 134

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