dynamini 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53c9e5cbb49d02855c69193b07ef3382bf94f632
4
- data.tar.gz: 3788e242f66c6bd679e84dc2801e4334180d2f3f
3
+ metadata.gz: b6793462d0bd5515eddf8c4108022460d76c55db
4
+ data.tar.gz: 33867af1d14657a57e66bc526b8e726eac5001b8
5
5
  SHA512:
6
- metadata.gz: d5e7b8a5c86e942f43abea007fe2f8d414c705180469407a6ed2bed4f6dfc9345921e763c47074e27ab1f1e181cfdb0ff8973135efd611702f9f6a43549fbe67
7
- data.tar.gz: 84f1fb83aa4db6dd6f3ffa9e34f89b74938dfd9ad218bfceec5a49e4776174cbf207e67948c2f99a21d3777a0c3e2d1e4debec763c81ad1bc72d138d8cbf3e86
6
+ metadata.gz: 7f3d2a791f4249a428f942a8934af442f0612ea6b520057f0bb2022d81a3c205df93a05838c6840397333153ba2be87f1e1a27ce17007d3afe4be960d1d86b61
7
+ data.tar.gz: 0ac3f2a81f739b53cce9ca24e4a788827dfbfb0b19d2693a13ba6485a1321127104d9aa07da7ef480457ae331666d9e713e1aef3f45d1ee5af2d57cae0caaa72
data/lib/dynamini.rb CHANGED
@@ -2,6 +2,7 @@ module Dynamini
2
2
  require 'active_model'
3
3
  require 'dynamini/base'
4
4
  require 'dynamini/configuration'
5
+ require 'dynamini/test_client'
5
6
 
6
7
  class << self
7
8
  attr_writer :configuration
@@ -14,12 +15,4 @@ module Dynamini
14
15
  def self.configure
15
16
  yield(configuration)
16
17
  end
17
- end
18
-
19
-
20
-
21
- # Dynamini.configure do |config|
22
- # config.aws_region = 'eu-west-1'
23
- # config.access_key_id =
24
- # config.secret_access_key =
25
- # end
18
+ end
data/lib/dynamini/base.rb CHANGED
@@ -6,7 +6,7 @@ module Dynamini
6
6
  BATCH_SIZE = 25
7
7
 
8
8
  class << self
9
- attr_writer :hash_key, :table_name, :batch_write_queue
9
+ attr_writer :hash_key, :table_name, :batch_write_queue, :in_memory
10
10
 
11
11
  def table_name
12
12
  @table_name || name.demodulize.downcase.pluralize
@@ -16,15 +16,23 @@ module Dynamini
16
16
  @hash_key || :id
17
17
  end
18
18
 
19
+ def in_memory
20
+ @in_memory || false
21
+ end
22
+
19
23
  def batch_write_queue
20
24
  @batch_write_queue ||= []
21
25
  end
22
26
 
23
27
  def client
24
- @client ||= Aws::DynamoDB::Client.new(
25
- region: Dynamini.configuration.region,
26
- access_key_id: Dynamini.configuration.access_key_id,
27
- secret_access_key: Dynamini.configuration.secret_access_key)
28
+ if in_memory
29
+ @client ||= Dynamini::TestClient.new(hash_key)
30
+ else
31
+ @client ||= Aws::DynamoDB::Client.new(
32
+ region: Dynamini.configuration.region,
33
+ access_key_id: Dynamini.configuration.access_key_id,
34
+ secret_access_key: Dynamini.configuration.secret_access_key)
35
+ end
28
36
  end
29
37
 
30
38
  def create(attributes, options={})
@@ -54,10 +62,14 @@ module Dynamini
54
62
 
55
63
  def batch_find(ids = [])
56
64
  return [] if ids.length < 1
65
+ objects = []
57
66
  raise StandardError, 'Batch find is limited to 100 items' if ids.length > 100
58
67
  key_structure = ids.map { |i| {hash_key => i} }
59
68
  response = self.dynamo_batch_get(key_structure)
60
- response.responses[table_name]
69
+ response.responses[table_name].each do |item|
70
+ objects << self.new(item.symbolize_keys, false)
71
+ end
72
+ objects
61
73
  end
62
74
 
63
75
  def enqueue_for_save(attributes, options = {})
@@ -158,11 +170,11 @@ module Dynamini
158
170
  end
159
171
 
160
172
  def save_to_dynamo
161
- Base.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: attribute_updates)
173
+ self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: attribute_updates)
162
174
  end
163
175
 
164
176
  def touch_to_dynamo
165
- Base.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: updated_at, action: 'PUT'}})
177
+ self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: updated_at, action: 'PUT'}})
166
178
  end
167
179
 
168
180
  def self.dynamo_batch_get(key_structure)
@@ -0,0 +1,64 @@
1
+ module Dynamini
2
+ require 'ostruct'
3
+
4
+ class TestClient
5
+
6
+ attr_reader :hash_key
7
+
8
+ def initialize(hash_key)
9
+ @data = {}
10
+ @hash_key = hash_key
11
+ end
12
+
13
+ def update_item(args = {})
14
+ table = args[:table_name]
15
+ @data[table] ||= {}
16
+ @data[table][args[:key][hash_key]] = flatten_attribute_updates(args[:attribute_updates])
17
+ @data[table][args[:key][hash_key]][hash_key] = args[:key][hash_key]
18
+ end
19
+
20
+ def get_item(args = {})
21
+ attributes_hash = @data[args[:table_name]][args[:key][hash_key]]
22
+ item = attributes_hash.nil? ? nil : attributes_hash
23
+ OpenStruct.new(item: item)
24
+
25
+ end
26
+
27
+ def batch_get_item(args = {})
28
+ responses = {}
29
+
30
+ args[:request_items].each do |k, v|
31
+ responses[k] = []
32
+ v[:keys].each do |key_hash|
33
+ item = @data[k][key_hash.values.first]
34
+ responses[k] << item
35
+ end
36
+ end
37
+
38
+ OpenStruct.new(responses: responses)
39
+ end
40
+
41
+ def batch_write_item(request_options)
42
+ request_options[:request_items].each do |k, v|
43
+ @data[k] ||= {}
44
+ v.each do |request_hash|
45
+ item = request_hash[:put_request][:item]
46
+ key = item[hash_key]
47
+ @data[k][key] = item
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def flatten_attribute_updates(attribute_updates)
55
+ attribute_hash = {}
56
+
57
+ attribute_updates.each do |k, v|
58
+ attribute_hash[k] = v[:value]
59
+ end
60
+ attribute_hash
61
+ end
62
+
63
+ end
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamini
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward
@@ -55,6 +55,20 @@ dependencies:
55
55
  - - ~>
56
56
  - !ruby/object:Gem::Version
57
57
  version: '3'
58
+ - !ruby/object:Gem::Dependency
59
+ name: pry
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ~>
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
58
72
  description: Lightweight DynamoDB interface designed as a drop-in replacement for
59
73
  ActiveRecord.
60
74
  email: dev@retailcommon.com
@@ -65,6 +79,7 @@ files:
65
79
  - lib/dynamini.rb
66
80
  - lib/dynamini/base.rb
67
81
  - lib/dynamini/configuration.rb
82
+ - lib/dynamini/test_client.rb
68
83
  homepage: https://github.com/47colborne/dynamini
69
84
  licenses:
70
85
  - MIT