mince_dynamo_db 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +2 -0
- data/lib/mince_dynamo_db/data_sanitizer.rb +11 -0
- data/lib/mince_dynamo_db/data_store.rb +12 -3
- data/lib/mince_dynamo_db/version.rb +1 -1
- data/spec/lib/data_sanitizer_spec.rb +40 -0
- data/spec/lib/data_store_spec.rb +12 -5
- metadata +5 -2
data/Guardfile
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
|
5
5
|
guard 'rspec', :version => 2 do
|
6
6
|
watch(%r{^spec/lib/.+_spec\.rb$})
|
7
|
+
watch(%r{^lib/.+\.rb$})
|
8
|
+
watch(%r{^lib/mince_dynamo_db/.+\.rb$})
|
7
9
|
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
8
10
|
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
9
11
|
watch(%r{^spec/support/shared_examples/(.+)\.rb$}) { "spec" }
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module MinceDynamoDb
|
2
|
+
module DataSanitizer
|
3
|
+
def self.prepare_field_for_storage(field)
|
4
|
+
field.is_a?(Numeric) ? field : field.to_s
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.prepare_hash_for_storage(hash)
|
8
|
+
hash.each{|key, value| hash[key] = prepare_field_for_storage(value) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -3,6 +3,7 @@ require 'digest'
|
|
3
3
|
require 'active_support/hash_with_indifferent_access'
|
4
4
|
|
5
5
|
require_relative 'connection'
|
6
|
+
require_relative 'data_sanitizer'
|
6
7
|
|
7
8
|
module MinceDynamoDb # :nodoc:
|
8
9
|
# = Mince DynamoDb Data Store
|
@@ -94,7 +95,7 @@ module MinceDynamoDb # :nodoc:
|
|
94
95
|
# @param [Hash] hash a hash of data to be added to the collection
|
95
96
|
def add(collection_name, hash)
|
96
97
|
hash.delete_if{|k,v| v.nil? }
|
97
|
-
items(collection_name).create(hash)
|
98
|
+
items(collection_name).create(sanitized_hash(hash))
|
98
99
|
end
|
99
100
|
|
100
101
|
# Replaces a record in the collection based on the primary key's value. The hash must contain a key, defined
|
@@ -104,7 +105,7 @@ module MinceDynamoDb # :nodoc:
|
|
104
105
|
# @param [String] collection_name the name of the collection
|
105
106
|
# @param [Hash] hash a hash to replace the record in the collection with
|
106
107
|
def replace(collection_name, hash)
|
107
|
-
items(collection_name).put(hash)
|
108
|
+
items(collection_name).put(sanitized_hash(hash))
|
108
109
|
end
|
109
110
|
|
110
111
|
# Gets all records that have the value for a given key.
|
@@ -164,7 +165,7 @@ module MinceDynamoDb # :nodoc:
|
|
164
165
|
# @param [*] value_to_push the value to push to the array
|
165
166
|
def push_to_array(collection_name, identifying_key, identifying_value, array_key, value_to_push)
|
166
167
|
item = items(collection_name).where(identifying_key.to_s => identifying_value).first
|
167
|
-
item.attributes.add(array_key => [value_to_push])
|
168
|
+
item.attributes.add(array_key => [sanitized_field(value_to_push)])
|
168
169
|
end
|
169
170
|
|
170
171
|
# Removes a value from a record's key that is an array
|
@@ -227,6 +228,14 @@ module MinceDynamoDb # :nodoc:
|
|
227
228
|
|
228
229
|
private
|
229
230
|
|
231
|
+
def sanitized_field(field)
|
232
|
+
DataSanitizer.prepare_field_for_storage(field)
|
233
|
+
end
|
234
|
+
|
235
|
+
def sanitized_hash(hash)
|
236
|
+
DataSanitizer.prepare_hash_for_storage(hash)
|
237
|
+
end
|
238
|
+
|
230
239
|
# Takes a DynamoDB item and returns the attributes of that item as a hash
|
231
240
|
def to_hash(item)
|
232
241
|
item.attributes.to_h if item
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative '../../lib/mince_dynamo_db/data_sanitizer'
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
describe MinceDynamoDb::DataSanitizer do
|
6
|
+
it 'converts a Time object to a string' do
|
7
|
+
value = Time.now
|
8
|
+
|
9
|
+
described_class.prepare_field_for_storage(value).should == value.to_s
|
10
|
+
described_class.prepare_hash_for_storage({ field: value }).should == { field: value.to_s }
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'does not convert a float' do
|
14
|
+
value = 3.2
|
15
|
+
|
16
|
+
described_class.prepare_field_for_storage(value).should == value
|
17
|
+
described_class.prepare_hash_for_storage({ field: value }).should == { field: value }
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'does not convert a string' do
|
21
|
+
value = 'string'
|
22
|
+
|
23
|
+
described_class.prepare_field_for_storage(value).should == value
|
24
|
+
described_class.prepare_hash_for_storage({ field: value }).should == { field: value }
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does not convert an integer' do
|
28
|
+
value = 1
|
29
|
+
|
30
|
+
described_class.prepare_field_for_storage(value).should == value
|
31
|
+
described_class.prepare_hash_for_storage({ field: value }).should == { field: value }
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'converts a Date object to a string' do
|
35
|
+
value = Date.today
|
36
|
+
|
37
|
+
described_class.prepare_field_for_storage(value).should == value.to_s
|
38
|
+
described_class.prepare_hash_for_storage({ field: value }).should == { field: value.to_s }
|
39
|
+
end
|
40
|
+
end
|
data/spec/lib/data_store_spec.rb
CHANGED
@@ -9,13 +9,17 @@ describe MinceDynamoDb::DataStore do
|
|
9
9
|
let(:collection_name) { 'some_collection_name'}
|
10
10
|
let(:primary_key) { mock 'primary key'}
|
11
11
|
let(:mock_id) { mock 'id' }
|
12
|
-
let(:data) { { :_id => mock_id}}
|
12
|
+
let(:data) { { :_id => mock_id, time_field: mock('time field')}}
|
13
|
+
let(:sanitized_data) { mock 'sanitized data' }
|
14
|
+
let(:sanitized_time) { mock 'sanitized time' }
|
15
|
+
let(:sanitized_id) { mock 'sanitized id' }
|
13
16
|
let(:return_data) { mock 'return data', attributes: attributes }
|
14
17
|
let(:attributes) { mock 'attributes', to_h: hash }
|
15
18
|
let(:hash) { mock 'hash' }
|
16
19
|
let(:items) { mock 'items' }
|
17
20
|
|
18
21
|
before do
|
22
|
+
MinceDynamoDb::DataSanitizer.stub(:prepare_hash_for_storage).with(data).and_return(sanitized_data)
|
19
23
|
MinceDynamoDb::Connection.stub(:instance => mince_dynamo_db_connection)
|
20
24
|
end
|
21
25
|
|
@@ -45,7 +49,7 @@ describe MinceDynamoDb::DataStore do
|
|
45
49
|
end
|
46
50
|
|
47
51
|
it 'can write to the collection' do
|
48
|
-
items.should_receive(:create).with(
|
52
|
+
items.should_receive(:create).with(sanitized_data).and_return(return_data)
|
49
53
|
|
50
54
|
subject.add(collection_name, data).should == return_data
|
51
55
|
end
|
@@ -60,7 +64,7 @@ describe MinceDynamoDb::DataStore do
|
|
60
64
|
end
|
61
65
|
|
62
66
|
it 'can replace a record' do
|
63
|
-
items.should_receive(:put).with(
|
67
|
+
items.should_receive(:put).with(sanitized_data)
|
64
68
|
|
65
69
|
subject.replace(collection_name, data)
|
66
70
|
end
|
@@ -118,10 +122,13 @@ describe MinceDynamoDb::DataStore do
|
|
118
122
|
end
|
119
123
|
|
120
124
|
it 'can push a value to an array for a specific record' do
|
125
|
+
value_to_push = mock 'value to push to the record'
|
126
|
+
sanitized_value_to_push = mock 'sanitized value to push to the record'
|
127
|
+
MinceDynamoDb::DataSanitizer.stub(:prepare_field_for_storage).with(value_to_push).and_return(sanitized_value_to_push)
|
121
128
|
items.should_receive(:where).with("key" => "value").and_return([return_data])
|
122
|
-
attributes.should_receive(:add).with(:array_key => [
|
129
|
+
attributes.should_receive(:add).with(:array_key => [sanitized_value_to_push])
|
123
130
|
|
124
|
-
subject.push_to_array(collection_name, :key, "value", :array_key,
|
131
|
+
subject.push_to_array(collection_name, :key, "value", :array_key, value_to_push)
|
125
132
|
end
|
126
133
|
|
127
134
|
it 'can remove a value from an array for a specific record' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mince_dynamo_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: aws-sdk
|
@@ -123,11 +123,13 @@ files:
|
|
123
123
|
- Rakefile
|
124
124
|
- lib/mince_dynamo_db.rb
|
125
125
|
- lib/mince_dynamo_db/connection.rb
|
126
|
+
- lib/mince_dynamo_db/data_sanitizer.rb
|
126
127
|
- lib/mince_dynamo_db/data_store.rb
|
127
128
|
- lib/mince_dynamo_db/version.rb
|
128
129
|
- mince_dynamo_db.gemspec
|
129
130
|
- spec/integration/persisting_to_dynamodb_test.rb
|
130
131
|
- spec/lib/connection_spec.rb
|
132
|
+
- spec/lib/data_sanitizer_spec.rb
|
131
133
|
- spec/lib/data_store_spec.rb
|
132
134
|
- spec/support/aws_example.yml
|
133
135
|
homepage: https://github.com/coffeencoke/mince_dynamo_db
|
@@ -157,6 +159,7 @@ summary: Lightweight ORM for Amazon's DynamoDB with Ruby Apps
|
|
157
159
|
test_files:
|
158
160
|
- spec/integration/persisting_to_dynamodb_test.rb
|
159
161
|
- spec/lib/connection_spec.rb
|
162
|
+
- spec/lib/data_sanitizer_spec.rb
|
160
163
|
- spec/lib/data_store_spec.rb
|
161
164
|
- spec/support/aws_example.yml
|
162
165
|
has_rdoc:
|