Class: DataMapper::Adapters::YamlAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm-yaml-adapter/adapter.rb

Instance Method Summary collapse

Instance Method Details

#attributes_as_fields(attributes) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/dm-yaml-adapter/adapter.rb', line 42

def attributes_as_fields(attributes)
  pairs = attributes.map do |property, value|
    dumped = value.kind_of?(Module) ? value.name : property.dump(value)
    [ property.field, dumped ]
  end
  Hash[pairs]
end

#create(resources) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/dm-yaml-adapter/adapter.rb', line 8

def create(resources)
  update_records(resources.first.model) do |records|
    resources.each do |resource|
      initialize_serial(resource, records.size.succ)
      records << attributes_as_fields(resource.attributes(nil))
    end
  end
end

#delete(collection) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/dm-yaml-adapter/adapter.rb', line 33

def delete(collection)
  update_records(collection.model) do |records|
    records_to_delete = collection.query.filter_records(records.dup)
    records.replace(records - records_to_delete)
    records_to_delete.size
  end
end

#read(query) ⇒ Object



18
19
20
# File 'lib/dm-yaml-adapter/adapter.rb', line 18

def read(query)
  query.filter_records(records_for(query.model).dup)
end

#update(attributes, collection) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/dm-yaml-adapter/adapter.rb', line 23

def update(attributes, collection)
  attributes = attributes_as_fields(attributes)

  update_records(collection.model) do |records|
    records_to_update = collection.query.filter_records(records.dup)
    records_to_update.each { |record| record.update(attributes) }.size
  end
end