dynamini 2.6.0 → 2.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c71e8851f1dcde97317c1fd5034423cb4698a823
4
- data.tar.gz: e62c81d4d011bc3bc99365dd577a6d11e054fdda
3
+ metadata.gz: eb80f52caa4a21c3f189d5427e7aa9ae90a1a2e0
4
+ data.tar.gz: 1c2f76b9cd36ccba731f263efabceeeafeb8ae97
5
5
  SHA512:
6
- metadata.gz: 65bb9e2e742706642974c9509100afc9c96fb1f71a1d06fb32413b342b7b61a613e4687c88ee4364ddd5b9663074794c028601eb4b5403720ce3395ca005ce7c
7
- data.tar.gz: acd5c9f3c9b30a2c5e512493348f7acc6fde4ffaa25cdd667519999b8f58ee7ca80613b0507da5b2cc03b9de92c622bcac8bc06bb9f3ddf901d3acea3da8c44e
6
+ metadata.gz: 0fc0ddf4c8c732c6943800a1fe735a1176203bb9f3740b2fb6cf762cfbe1d614bb684c7b34c32b9f04893668bc66f7745aa6d93addb55b9c199c09de81866a2e
7
+ data.tar.gz: a658de15ef8a2b607b68f7799d3ed23262df8df223fbfc838039e310a3986cfba8a99392fe0b5642eb56de2b38b4aa3d221fa244b22af109a34cd5dc5968d7dc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dynamini (2.6.0)
4
+ dynamini (2.6.2)
5
5
  activemodel (>= 3, < 5.0)
6
6
  aws-sdk (~> 2)
7
7
 
data/README.md CHANGED
@@ -43,6 +43,7 @@ There are also some new functions specific to DynamoDB's API:
43
43
  * find_or_nil(hash_key, range_key) - since ActiveRecord's find_by isn't applicable to noSQL, use this method if you want a .find that doesn't raise exceptions when the item doesn't exist
44
44
  * batch_find([keys]) - to retrieve multiple objects at once.
45
45
  * increment!({attribute1: amount, attribute2: amount}) - to update your record using DynamoDB's Atomic Counter functionality. (For more information, see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters )
46
+ * add_to(attribute, value) - If you use this to modify your attribute, when saving, Dynamini will update that attribute with ADD instead of PUT. Your attribute must be handled as an addable type - :integer, :float, :array, :set, :date, or :time. (For more information on ADD actions, see http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html )
46
47
 
47
48
  ## Configuration
48
49
  In application.rb, or in initializers/dynamini.rb, include your AWS settings like so:
@@ -99,12 +100,13 @@ The following datatypes are supported by handle:
99
100
  * :time
100
101
  * :string
101
102
  * :array
103
+ * :set
102
104
 
103
105
  Booleans and strings don't actually need to be translated, but you can set up defaults for those fields this way.
104
106
  The magic fields updated_at and created_at are handled as :time by default.
105
107
 
106
- ## Array Support
107
- You can save arrays to your Dynamini model. Optionally, you can have Dynamini perform type conversion on each element of your array. Here's how it works:
108
+ ## Enumerable Support
109
+ You can save arrays and sets to your Dynamini model. Optionally, you can have Dynamini perform type conversion on each element of your enumerable. Here's how it works:
108
110
 
109
111
  ```ruby
110
112
  class Vehicle < Dynamini::Base
@@ -138,7 +140,7 @@ Vehicle.find('H3LLO').other_array
138
140
  > ['wheel', 'brakes', BigDecimal(5)]
139
141
  ```
140
142
 
141
- Please note that changing arrays in place using mutator methods like << or map! will not record a change to the object.
143
+ Please note that changing enumerables in place using mutator methods like << or map! will not record a change to the object.
142
144
 
143
145
  If you want to make changes like this, either clone it then use the assignment operator (e.g. model.array = model.array.dup << 'foo') or call model.mark(:attribute) after mutation and before saving to force Dynamini to write the change.
144
146
 
@@ -222,7 +224,7 @@ config.after(:each) {
222
224
  * You can also write any arbitrary attribute to your model.
223
225
  * Other models in your app cannot have a has_one or has_many relationship with your Dynamini model, since these would require a table scan. Your other models can still use belongs_to.
224
226
  * If you change the primary key value on an instance of your model, then resave it, you'll have two copies in your database.
225
- * If you use non-numeric strings for your primary key, remember to change your foreign key columns on related objects to be string type.
227
+ * If you use non-numeric strings for your primary key, remember to change your foreign key columns on related ActiveRecord tables to be string type.
226
228
  * You might want to conditionally set the table name for your model based on the Rails.env, enabling separate tables for development and production.
227
229
 
228
230
  ## Contributing
data/dynamini.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dynamini'
3
- s.version = '2.6.0'
3
+ s.version = '2.6.2'
4
4
  s.summary = 'DynamoDB interface'
5
5
  s.description = 'Lightweight DynamoDB interface gem designed as
6
6
  a drop-in replacement for ActiveRecord.
@@ -9,8 +9,8 @@ module Dynamini
9
9
  symbol: proc { |v| v.to_sym },
10
10
  string: proc { |v| v },
11
11
  boolean: proc { |v| v },
12
- array: proc { |v| v },
13
- set: proc { |v| v }
12
+ array: proc { |v| v.to_a },
13
+ set: proc { |v| Set.new(v) }
14
14
  }
15
15
 
16
16
  SETTER_PROCS = {
@@ -21,8 +21,8 @@ module Dynamini
21
21
  string: proc { |v| v },
22
22
  boolean: proc { |v| v },
23
23
  date: proc { |v| v.to_time.to_f },
24
- array: proc { |v| v },
25
- set: proc { |v| v }
24
+ array: proc { |v| v.to_a },
25
+ set: proc { |v| Set.new(v) }
26
26
  }
27
27
 
28
28
  module ClassMethods
@@ -71,13 +71,11 @@ module Dynamini
71
71
  end
72
72
 
73
73
  def attribute_callback(procs, handle, value)
74
- type = handle[:options][:of] || handle[:format]
75
- callback = procs[type]
76
- if value.is_a?(Array)
77
- value.map { |e| callback.call(e) }
78
- elsif value.is_a?(Set)
79
- Set.new(value.map { |e| callback.call(e) })
80
- elsif handled_as?(handle, [:array, :set])
74
+ callback = procs[handle[:format]]
75
+ if should_convert_elements?(handle, value)
76
+ result = convert_elements(value, procs[handle[:options][:of]])
77
+ callback.call(result)
78
+ elsif invalid_enumerable_value?(handle, value)
81
79
  raise ArgumentError, "Can't write a non-enumerable value to field handled as #{handle[:format]}"
82
80
  else
83
81
  callback.call(value)
@@ -92,5 +90,17 @@ module Dynamini
92
90
  base.extend ClassMethods
93
91
  end
94
92
 
93
+ def should_convert_elements?(handle, value)
94
+ handle[:options][:of] && (value.is_a?(Array) || value.is_a?(Set))
95
+ end
96
+
97
+ def invalid_enumerable_value?(handle, value)
98
+ handled_as?(handle, [:array, :set]) && !value.is_a?(Enumerable)
99
+ end
100
+
101
+ def convert_elements(enumerable, callback)
102
+ enumerable.map { |e| callback.call(e) }
103
+ end
104
+
95
105
  end
96
106
  end
@@ -16,10 +16,6 @@ describe Dynamini::TypeHandler do
16
16
  object = HandledClass.new
17
17
  expect(object.price).to eq(9)
18
18
  end
19
- it 'should return an array with formatted items if handled' do
20
- object = HandledClass.new(price: ["1", "2"])
21
- expect(object.price).to eq([1, 2])
22
- end
23
19
  end
24
20
 
25
21
  context 'when writing the handled attribute' do
@@ -101,6 +97,11 @@ describe Dynamini::TypeHandler do
101
97
  it 'should allow default values for arrays' do
102
98
  expect(handle_model.defaulted_ary).to eq([1, 2, 3])
103
99
  end
100
+
101
+ it 'should convert sets to arrays' do
102
+ handle_model.float_array = Set.new([12,24,48])
103
+ expect(handle_model.float_array).to_not be_a(Set)
104
+ end
104
105
  end
105
106
 
106
107
  context 'when handling as set' do
@@ -121,17 +122,10 @@ describe Dynamini::TypeHandler do
121
122
  it 'should allow default values for arrays' do
122
123
  expect(handle_model.defaulted_ary).to eq([1, 2, 3])
123
124
  end
124
- end
125
-
126
- context 'legacy support' do
127
- it 'should save casted arrays' do
128
- handle_model.int_list = [12, 24, 48]
129
- expect(handle_model.int_list).to eq([12, 24, 48])
130
- end
131
125
 
132
- it 'should retrieve casted arrays' do
133
- handle_model.sym_list = ['foo', 'bar', 'baz']
134
- expect(handle_model.sym_list).to eq([:foo, :bar, :baz])
126
+ it 'should convert arrays to sets' do
127
+ handle_model.float_set = [12,24,48]
128
+ expect(handle_model.float_set).to_not be_a(Array)
135
129
  end
136
130
  end
137
131
  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: 2.6.0
4
+ version: 2.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2016-09-21 00:00:00.000000000 Z
18
+ date: 2016-09-22 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activemodel