Class: Restful::Adapter

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

Instance Method Summary collapse

Instance Method Details

#create(resources) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dm-restful-adapter/adapter.rb', line 4

def create(resources)
  resources.map { |resource|
    dirty_attributes = resource.dirty_attributes.inject({}) { |hash, (k,v)| hash.update(k.name => v) }
    response = Request.post(resource.model.storage_name, dirty_attributes)
    if response.status == 200
      properties =  resource.model.properties
      response.body.each do |attr_name, val|
        if prop = properties[attr_name.to_sym]
          prop.set!(resource, val)
        end
      end
    end
  }.compact.size
end

#delete(resources) ⇒ Object



38
39
40
41
42
43
# File 'lib/dm-restful-adapter/adapter.rb', line 38

def delete(resources)
  resources.map do |resource|
    response = Request.delete(resource.model.storage_name, resource.key)
    response.status == 200 ? response.body : nil
  end.compact.size
end

#read(query) ⇒ Object



19
20
21
# File 'lib/dm-restful-adapter/adapter.rb', line 19

def read(query)
  Request.get(query.model.storage_name, query.params).body
end

#update(attrs, resources) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dm-restful-adapter/adapter.rb', line 23

def update(attrs, resources)
  resources.map do |resource|
    attr_hash = attrs.inject({}) { |h, (k, v)| h.merge(k.name => v) }
    response = Request.put(resource.model.storage_name, resource.key, attr_hash)
    if response.status == 200
      properties =  resource.model.properties.inspect
      response.body.except(:id).each do |k, v|
        if prop = properties[k.to_sym]
          prop.set!(resource, v)
        end
      end
    end
  end.compact.size
end