toy-dynamo 0.0.15 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/toy/dynamo/table.rb +82 -2
- data/lib/toy/dynamo/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 379a504380d30c3c6b1c38c47661e7f366a3a6ae
|
4
|
+
data.tar.gz: d7b3c58d0d7a2f4bca65d234b542e295cc4b9c56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27692d7df92daba4828fb3bb122ef684e50a378b307090341e9c2ac653e42c13a329b23c1f0ee2497fe351913e6a27674e09770328cd35bc767bfe4bd8c238fa
|
7
|
+
data.tar.gz: 4f8a7e8b64b3c16862fb83f02db97405c470bbbfe57464f926b94b13b17f3254ad63e963ed6b163979b577e7ac86b7203e7af382e4babd03564fa82edf414453
|
data/lib/toy/dynamo/table.rb
CHANGED
@@ -31,9 +31,25 @@ module Toy
|
|
31
31
|
:ge => "GE",
|
32
32
|
:gt => "GT",
|
33
33
|
:begins_with => "BEGINS_WITH",
|
34
|
-
:between => "BETWEEN"
|
34
|
+
:between => "BETWEEN",
|
35
|
+
# Scan only
|
36
|
+
:ne => "NE",
|
37
|
+
:not_null => "NOT_NULL",
|
38
|
+
:null => "NULL",
|
39
|
+
:contains => "CONTAINS",
|
40
|
+
:not_contains => "NOT_CONTAINS",
|
41
|
+
:in => "IN"
|
35
42
|
}
|
36
43
|
|
44
|
+
COMPARISON_OPERATOR_SCAN_ONLY = [
|
45
|
+
:ne,
|
46
|
+
:not_null,
|
47
|
+
:null,
|
48
|
+
:contains,
|
49
|
+
:not_contains,
|
50
|
+
:in
|
51
|
+
]
|
52
|
+
|
37
53
|
def initialize(table_schema, client, options={})
|
38
54
|
@table_schema = table_schema
|
39
55
|
@client = client
|
@@ -139,7 +155,7 @@ module Toy
|
|
139
155
|
if options[:range]
|
140
156
|
raise ArgumentError, "Expected a 1 element Hash for :range (ex {:age.gt => 13})" unless options[:range].is_a?(Hash) && options[:range].keys.size == 1
|
141
157
|
range_key_name, comparison_operator = options[:range].keys.first.split(".")
|
142
|
-
raise ArgumentError, "Comparison operator must be one of (#{COMPARISON_OPERATOR.keys.join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym)
|
158
|
+
raise ArgumentError, "Comparison operator must be one of (#{(COMPARISON_OPERATOR.keys - COMPARISON_OPERATOR_SCAN_ONLY).join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym)
|
143
159
|
range_key = @range_keys.find{|k| k[:attribute_name] == range_key_name}
|
144
160
|
raise ArgumentError, ":range key must be a valid Range attribute" unless range_key
|
145
161
|
raise ArgumentError, ":range key must be a Range if using the operator BETWEEN" if comparison_operator == "between" && !options[:range].values.first.is_a?(Range)
|
@@ -325,6 +341,70 @@ module Toy
|
|
325
341
|
end
|
326
342
|
end
|
327
343
|
|
344
|
+
# Perform a table scan
|
345
|
+
# http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
|
346
|
+
def scan(options={})
|
347
|
+
options[:return_consumed_capacity] ||= :none # "NONE" # || "TOTAL"
|
348
|
+
# Default if not already set
|
349
|
+
options[:select] ||= :all # :all, :projected, :count, []
|
350
|
+
|
351
|
+
scan_request = {
|
352
|
+
:table_name => options[:table_name] || self.table_name,
|
353
|
+
:return_consumed_capacity => RETURNED_CONSUMED_CAPACITY[options[:return_consumed_capacity]]
|
354
|
+
}
|
355
|
+
|
356
|
+
scan_request.merge!({ :limit => options[:limit].to_i }) if options.has_key?(:limit)
|
357
|
+
scan_request.merge!({ :exclusive_start_key => options[:exclusive_start_key] }) if options[:exclusive_start_key]
|
358
|
+
|
359
|
+
if options[:select].is_a?(Array)
|
360
|
+
attrs_to_select = [options[:select].map(&:to_s)].flatten
|
361
|
+
attrs_to_select << @hash_key[:attribute_name]
|
362
|
+
attrs_to_select << @range_keys.find{|k| k[:primary_range_key] }[:attribute_name] if @range_keys
|
363
|
+
scan_request.merge!({
|
364
|
+
:select => QUERY_SELECT[:specific],
|
365
|
+
:attributes_to_get => attrs_to_select.uniq
|
366
|
+
})
|
367
|
+
else
|
368
|
+
scan_request.merge!({ :select => QUERY_SELECT[options[:select]] })
|
369
|
+
end
|
370
|
+
|
371
|
+
# :scan_filter => { :name.begins_with => "a" }
|
372
|
+
scan_filter = {}
|
373
|
+
if options[:scan_filter].present?
|
374
|
+
options[:scan_filter].each_pair.each do |k,v|
|
375
|
+
# Hard to validate attribute types here, so infer by type sent and assume the user knows their own attrs
|
376
|
+
key_name, comparison_operator = k.split(".")
|
377
|
+
raise ArgumentError, "Comparison operator must be one of (#{COMPARISON_OPERATOR.keys.join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym)
|
378
|
+
raise ArgumentError, "scan_filter value must be a Range if using the operator BETWEEN" if comparison_operator == "between" && !v.is_a?(Range)
|
379
|
+
raise ArgumentError, "scan_filter value must be a Array if using the operator IN" if comparison_operator == "in" && !v.is_a?(Array)
|
380
|
+
|
381
|
+
attribute_value_list = []
|
382
|
+
if comparison_operator == "in"
|
383
|
+
v.each do |in_v|
|
384
|
+
attribute_value_list << attr_with_type(key_name, in_v).values.last
|
385
|
+
end
|
386
|
+
elsif comparison_operator == "between"
|
387
|
+
attribute_value_list << attr_with_type(key_name, range_value.min).values.last
|
388
|
+
attribute_value_list << attr_with_type(key_name, range_value.max).values.last
|
389
|
+
else
|
390
|
+
attribute_value_list << attr_with_type(key_name, v).values.last
|
391
|
+
end
|
392
|
+
scan_filter.merge!({
|
393
|
+
key_name => {
|
394
|
+
:comparison_operator => COMPARISON_OPERATOR[comparison_operator.to_sym],
|
395
|
+
:attribute_value_list => attribute_value_list
|
396
|
+
}
|
397
|
+
})
|
398
|
+
end
|
399
|
+
scan_request.merge!(:scan_filter => scan_filter)
|
400
|
+
end
|
401
|
+
|
402
|
+
scan_request.merge!({ :segment => options[:segment] }) if options[:segment]
|
403
|
+
scan_request.merge!({ :total_segments => options[:total_segments].to_i }) if options[:total_segments]
|
404
|
+
|
405
|
+
@client.scan(scan_request)
|
406
|
+
end
|
407
|
+
|
328
408
|
# Call proc or return string
|
329
409
|
def table_name
|
330
410
|
if @table_schema[:table_name].respond_to?(:call)
|
data/lib/toy/dynamo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toy-dynamo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cary Dunn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
requirements: []
|
137
137
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.0.
|
138
|
+
rubygems_version: 2.0.2
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: ActiveModel based DynamoDB ORM - extension to toystore
|