Class: Dynamini::TestClient
- Inherits:
-
Object
- Object
- Dynamini::TestClient
- Defined in:
- lib/dynamini/test_client.rb
Overview
In-memory database client for test purposes.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#hash_key ⇒ Object
readonly
Returns the value of attribute hash_key.
-
#range_key ⇒ Object
readonly
Returns the value of attribute range_key.
Instance Method Summary collapse
- #batch_get_item(args = {}) ⇒ Object
- #batch_write_item(request_options) ⇒ Object
- #delete_item(args = {}) ⇒ Object
- #get_item(args = {}) ⇒ Object
-
#initialize(hash_key, range_key = nil) ⇒ TestClient
constructor
A new instance of TestClient.
- #reset ⇒ Object
- #update_item(args = {}) ⇒ Object
Constructor Details
#initialize(hash_key, range_key = nil) ⇒ TestClient
Returns a new instance of TestClient.
9 10 11 12 13 |
# File 'lib/dynamini/test_client.rb', line 9 def initialize(hash_key, range_key=nil) @data = {} @hash_key = hash_key @range_key = range_key end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/dynamini/test_client.rb', line 7 def data @data end |
#hash_key ⇒ Object (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 |
#range_key ⇒ Object (readonly)
Returns the value of attribute range_key.
7 8 9 |
# File 'lib/dynamini/test_client.rb', line 7 def range_key @range_key end |
Instance Method Details
#batch_get_item(args = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dynamini/test_client.rb', line 69 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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dynamini/test_client.rb', line 83 def batch_write_item() [: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
94 95 96 |
# File 'lib/dynamini/test_client.rb', line 94 def delete_item(args = {}) @data[args[:table_name]].delete(args[:key][hash_key]) end |
#get_item(args = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dynamini/test_client.rb', line 51 def get_item(args = {}) table = args[:table_name] hash_key_value = args[:key][hash_key] range_key_value = args[:key][range_key] @data[table] ||= {} if hash_key_value && range_key_value attributes_hash = @data[table][hash_key_value] attributes_hash = attributes_hash[range_key_value] if attributes_hash else attributes_hash = @data[table][hash_key_value] end item = attributes_hash.nil? ? nil : attributes_hash OpenStruct.new(item: item) end |
#reset ⇒ Object
98 99 100 |
# File 'lib/dynamini/test_client.rb', line 98 def reset @data = {} end |
#update_item(args = {}) ⇒ Object
15 16 17 18 19 20 21 22 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 |
# File 'lib/dynamini/test_client.rb', line 15 def update_item(args = {}) table = args[:table_name] arg_keys = args[:key] arg_hash_key_str = arg_keys[hash_key] arg_range_key_str = arg_keys[range_key] updates = flatten_attribute_updates(args).merge( hash_key => arg_hash_key_str ) @data[table] ||= {} #existing record for hash && range if @data[table][arg_hash_key_str].present? && arg_keys[hash_key].present? && @range_key.present? && arg_keys[range_key].present? updates.merge!(range_key => arg_range_key_str) @data[table][arg_hash_key_str][arg_range_key_str].merge! updates #new record for hash & range ONLY elsif arg_keys[hash_key].present? && arg_keys[range_key].present? updates.merge!(range_key => arg_range_key_str) @data[table][arg_hash_key_str] ||= {} @data[table][arg_hash_key_str][arg_range_key_str] = updates #existing record for hash ONLY elsif @data[table][arg_hash_key_str].present? @data[table][arg_hash_key_str].merge!(updates) #new record for hash ONLY elsif arg_keys[hash_key].present? @data[table][arg_hash_key_str] = updates end end |