Class: Dynamini::TestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamini/test_client.rb

Overview

In-memory database client for test purposes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_key) ⇒ TestClient

Returns a new instance of TestClient.



9
10
11
12
# File 'lib/dynamini/test_client.rb', line 9

def initialize(hash_key)
  @data = {}
  @hash_key = hash_key
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/dynamini/test_client.rb', line 7

def data
  @data
end

#hash_keyObject (readonly)

Returns the value of attribute hash_key.



7
8
9
# File 'lib/dynamini/test_client.rb', line 7

def hash_key
  @hash_key
end

Instance Method Details

#batch_get_item(args = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dynamini/test_client.rb', line 35

def batch_get_item(args = {})
  responses = {}

  args[:request_items].each do |k, v|
    responses[k] = []
    v[:keys].each do |key_hash|
      item = @data[k][key_hash.values.first]
      responses[k] << item
    end
  end

  OpenStruct.new(responses: responses)
end

#batch_write_item(request_options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/dynamini/test_client.rb', line 49

def batch_write_item(request_options)
  request_options[:request_items].each do |k, v|
    @data[k] ||= {}
    v.each do |request_hash|
      item = request_hash[:put_request][:item]
      key = item[hash_key]
      @data[k][key] = item
    end
  end
end

#delete_item(args = {}) ⇒ Object



60
61
62
# File 'lib/dynamini/test_client.rb', line 60

def delete_item(args = {})
  @data[args[:table_name]].delete(args[:key][hash_key])
end

#get_item(args = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/dynamini/test_client.rb', line 27

def get_item(args = {})
  table = args[:table_name]
  @data[table] ||= {}
  attributes_hash = @data[table][args[:key][hash_key]]
  item = attributes_hash.nil? ? nil : attributes_hash
  OpenStruct.new(item: item)
end

#resetObject



64
65
66
# File 'lib/dynamini/test_client.rb', line 64

def reset
  @data = {}
end

#update_item(args = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dynamini/test_client.rb', line 14

def update_item(args = {})
  table = args[:table_name]
  updates = flatten_attribute_updates(args).merge(
      hash_key => args[:key][hash_key]
  )
  @data[table] ||= {}
  if @data[table][args[:key][hash_key]].present?
    @data[table][args[:key][hash_key]].merge!(updates)
  else
    @data[table][args[:key][hash_key]] = updates
  end
end