Class: DataMapper::WAZTables::Adapter

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

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Adapter

Returns a new instance of Adapter.



5
6
7
8
# File 'lib/dm-waztables-adapter/adapter.rb', line 5

def initialize(name, options = {})
  super
  WAZ::Storage::Base.establish_connection!(:account_name => options[:account_name], :access_key => options[:access_key])
end

Instance Method Details

#create(resources) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/dm-waztables-adapter/adapter.rb', line 11

def create(resources)
  service = WAZ::Tables::Table.service_instance
  resources.each do |resource|
    entity = {:row_key => resource.key.first}
    entity.merge!({:partition_key => resource.model.storage_name})
    resource.attributes.each { |a| entity.merge!({ a[0].to_sym => a[1] }) }
    service.insert_entity(resource.model.storage_name, entity)
  end
  resources.size
end

#delete(resources) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dm-waztables-adapter/adapter.rb', line 73

def delete(resources)
  service = WAZ::Tables::Table.service_instance
  resources.each do |resource|
    service.delete_entity(resource.model.storage_name, resource.model.storage_name, resource.id)
  end
  resources.size
end

#read(query) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dm-waztables-adapter/adapter.rb', line 23

def read(query)
  service = WAZ::Tables::Table.service_instance
  operator_mapping = {:gt => 'gt', :lt => 'lt', :gte => 'ge', :lte => 'le', :not => 'ne', :eql => 'eq'}

  conditions = query.conditions.map do |c|
    unless (c.class.name.eql?('Array'))
      case c.slug
        when :not then 
          "(#{c.operand.to_s.gsub(%r{=}, operator_mapping[c.slug]).gsub(%r{"}, "'")})"              
        when :like then
          # 
          name, value = c.subject.name, c.value.delete('%')
          (c.value =~ /^%/) ? "endswith(#{name}, '#{value}')" : ((c.value =~ /%$/) ? "startswith(#{name}, '#{value}')" : "(#{name} eq '#{value}')")
        when :regexp then
        else 
          "(#{c.subject.name} #{operator_mapping[c.slug]} #{c.value.class.name =~ /Fixnum|Float|Bignum/ ? c.value : "'#{c.value}'"})"
      end
    else
      query.conditions
    end
  end

  query_options = {:expression => conditions.sort.compact.join(' and ')}
  query_options.merge!({:top => query.limit}) if query.limit

  result = service.query(query.model.storage_name, query_options)

  records = result.map do |item|
    record = {}
    item.each { |k,v| record.merge!( { k.to_s => v } ) }
    record
  end
  
  query.filter_records(records)
end

#update(attributes, resources) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dm-waztables-adapter/adapter.rb', line 60

def update(attributes, resources)
  service = WAZ::Tables::Table.service_instance
  resources.each do |resource|
    entity = service.get_entity(resource.model.storage_name, resource.model.storage_name, resource.id)
    attributes.each do |attribute|
      entity[attribute[0].name] = attribute[1]
    end
    entity = service.update_entity(resource.model.storage_name, entity)
  end
  resources.size
end