Class: DataMapper::Adapters::InMemoryAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#field_naming_convention, #name, #resource_naming_convention, #uri

Instance Method Summary collapse

Methods included from AbstractAdapter::Migration::ClassMethods

#type_map

Methods included from AbstractAdapter::Transaction

#current_transaction, #pop_transaction, #push_transaction, #transaction_primitive, #within_transaction?

Methods included from AbstractAdapter::Migration

#alter_model_storage, #alter_property_storage, #create_model_storage, #create_property_storage, #destroy_model_storage, #destroy_property_storage, #field_exists?, #storage_exists?, #upgrade_model_storage

Methods included from DataMapper::Assertions

#assert_kind_of

Constructor Details

#initialize(name, uri_or_options) ⇒ InMemoryAdapter

Returns a new instance of InMemoryAdapter.



4
5
6
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 4

def initialize(name, uri_or_options)
  @records = Hash.new { |hash,model| hash[model] = Array.new }
end

Instance Method Details

#create(resources) ⇒ Object



8
9
10
11
12
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 8

def create(resources)
  resources.each do |resource|
    @records[resource.model] << resource
  end.size # just return the number of records
end

#delete(query) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 32

def delete(query)
  records = @records[query.model]

  read_many(query).each do |resource|
    records.delete(resource)
  end.size
end

#read_many(query) ⇒ Object



26
27
28
29
30
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 26

def read_many(query)
  Collection.new(query) do |set|
    read(query, set, true)
  end
end

#read_one(query) ⇒ Object



22
23
24
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 22

def read_one(query)
  read(query, query.model, false)
end

#update(attributes, query) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/adapters/in_memory_adapter.rb', line 14

def update(attributes, query)
  read_many(query).each do |resource|
    attributes.each do |property,value|
      property.set!(resource, value)
    end
  end.size
end