Module: Toy::Dynamo::Adapter

Defined in:
lib/toy/dynamo/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_client(config = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/toy/dynamo/adapter.rb', line 7

def self.default_client(config={})
  options={}
  options[:use_ssl] = Toy::Dynamo::Config.use_ssl
  options[:use_ssl] = config[:use_ssl] if config.has_key?(:use_ssl)
  options[:dynamo_db_endpoint] = config[:endpoint] || Toy::Dynamo::Config.endpoint
  options[:dynamo_db_port] = config[:port] || Toy::Dynamo::Config.port

  options[:api_version] ||= config[:api_version] || '2012-08-10'
  #:dynamo_db_crc_32_check = false

  @@default_client ||= AWS::DynamoDB::Client.new(options)
end

Instance Method Details

#batch_read(keys, options = nil) ⇒ Object



39
40
41
42
# File 'lib/toy/dynamo/adapter.rb', line 39

def batch_read(keys, options=nil)
  options ||= {}
  @options[:model].dynamo_table.batch_get_item(keys, options)
end

#clear(options = nil) ⇒ Object



54
55
56
# File 'lib/toy/dynamo/adapter.rb', line 54

def clear(options=nil)
  @options[:model].dynamo_table.delete
end

#delete(key, options = nil) ⇒ Object



49
50
51
52
# File 'lib/toy/dynamo/adapter.rb', line 49

def delete(key, options=nil)
  options ||= {}
  @options[:model].dynamo_table.delete_item(key, options)
end

#read(key, options = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/toy/dynamo/adapter.rb', line 20

def read(key, options=nil)
  options ||= {}
  attrs = nil
  if @options[:model].dynamo_table.range_keys.present?
    raise ArgumentError, "Expected :range_value option" unless options[:range_value].present?
    result = @options[:model].dynamo_table.query(key, options.merge(
      :range => {
        "#{@options[:model].dynamo_table.range_keys.find{|k| k[:primary_range_key] }[:attribute_name]}".to_sym.eq => options[:range_value]
      }
    ))
    attrs = (result[:member].empty? ? nil : Response.strip_attr_types(result[:member].first))
  else
    result = @options[:model].dynamo_table.get_item(key, options)
    attrs = (result.try(:[], :item).blank? ? nil : Response.strip_attr_types(result[:item]))
  end

  attrs
end

#write(key, attributes, options = nil) ⇒ Object



44
45
46
47
# File 'lib/toy/dynamo/adapter.rb', line 44

def write(key, attributes, options=nil)
  options ||= {}
  @options[:model].dynamo_table.write(key, attributes, options)
end