Class: DataMapper::Adapters::YamlAdapter

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) attributes_as_fields(attributes)



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

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

- (Object) create(resources)



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

def create(resources)
  model = resources.first.model
  update_records(model) do |records|
    max_id = max_id_for(model, records)
    resources.each do |resource|
      initialize_serial(resource, max_id += 1)
      records << attributes_as_fields(resource.attributes(nil))
    end
  end
end

- (Object) delete(collection)



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

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

- (Object) read(query)



20
21
22
# File 'lib/dm-yaml-adapter/adapter.rb', line 20

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

- (Object) update(attributes, collection)



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

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