rdy 0.1.3 → 0.2.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.
- data/README.md +6 -1
- data/VERSION +1 -1
- data/lib/rdy.rb +59 -10
- data/test/helper.rb +6 -2
- data/test/test_rdy.rb +97 -9
- metadata +5 -5
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Fun little ruby client for Amazon DynamoDB.
|
4
4
|
|
5
|
-
rdy = Rdy.new("your_table",
|
5
|
+
rdy = Rdy.new("your_table", [:your_hash_key, :string])
|
6
6
|
rdy.any_attribute = "nice!"
|
7
7
|
rdy.foo = "bar"
|
8
8
|
rdy.save("1") # hash key value
|
@@ -17,6 +17,11 @@ Fun little ruby client for Amazon DynamoDB.
|
|
17
17
|
rdy.destroy # delete item
|
18
18
|
|
19
19
|
rdy.all
|
20
|
+
rdy.count
|
21
|
+
|
22
|
+
rdy.scan(:any_attribute => 'nice!')
|
23
|
+
limit = 10
|
24
|
+
rdy.scan(:any_attribute => 'nice!', 10)
|
20
25
|
|
21
26
|
read_capacity_units = 10
|
22
27
|
write_capacity_units = 5
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/rdy.rb
CHANGED
@@ -3,17 +3,38 @@ gem "aws-sdk"
|
|
3
3
|
require "aws-sdk"
|
4
4
|
|
5
5
|
class Rdy
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
attr_accessor :hash_key_conditional_check, :check_table_status
|
7
|
+
@@_tables = {}
|
8
|
+
|
9
|
+
def initialize(table, hash_key, range_key = nil)
|
10
|
+
@attributes = {}; @table = table; @hash_key = hash_key[0].to_s
|
11
|
+
@range_key = range_key[0].to_s if range_key
|
12
|
+
@is_new = true
|
13
|
+
self.hash_key_conditional_check = false
|
14
|
+
self.check_table_status = false
|
15
|
+
if @@_tables[table]
|
16
|
+
@_table = @@_tables[table]
|
17
|
+
else
|
18
|
+
@_table = Rdy.dynamo_db.tables[@table]
|
19
|
+
if self.check_table_status
|
20
|
+
if @_table.status == :active
|
21
|
+
@@_tables[table] = @_table
|
22
|
+
else
|
23
|
+
raise "Table not active yet!"
|
24
|
+
end
|
25
|
+
else
|
26
|
+
@_table.hash_key = [@hash_key.to_sym, hash_key[1].to_sym]
|
27
|
+
@_table.range_key = [@range_key.to_sym, range_key[1].to_sym] if @range_key
|
28
|
+
end
|
29
|
+
end
|
10
30
|
end
|
11
31
|
def table=(value); @table = value; end
|
12
32
|
def table; @table; end
|
13
33
|
def attributes; @attributes; end
|
14
34
|
def hash_value; @hash_value; end
|
15
35
|
def hash_key; @hash_key; end
|
16
|
-
|
36
|
+
def range_key; @range_key; end
|
37
|
+
|
17
38
|
def self.dynamo_db
|
18
39
|
config = YAML.load(File.read("#{ENV['HOME']}/.rdy.yml"))
|
19
40
|
raise "Config file expected in ~/.rdy.yml" unless config
|
@@ -26,11 +47,15 @@ class Rdy
|
|
26
47
|
end
|
27
48
|
|
28
49
|
def all; @_table.items.collect {|i| i.attributes.to_h }; end
|
29
|
-
def find(hash_value)
|
50
|
+
def find(hash_value, range_value = nil)
|
30
51
|
raise "missing hash value" if hash_value.nil?
|
31
|
-
@
|
52
|
+
if @range_key and range_value
|
53
|
+
@_item = @_table.items.at(@hash_value, range_value)
|
54
|
+
else
|
55
|
+
@_item = @_table.items[hash_value]
|
56
|
+
end
|
32
57
|
@attributes.clear
|
33
|
-
if @_item.attributes.any?
|
58
|
+
if @_item and @_item.attributes and @_item.attributes.any?
|
34
59
|
@_item.attributes.to_h.each {|k, v| self.send("#{k}=".to_sym, v) unless k == @hash_key }
|
35
60
|
@hash_value = hash_value; @is_new = false
|
36
61
|
else
|
@@ -38,19 +63,43 @@ class Rdy
|
|
38
63
|
end
|
39
64
|
@attributes
|
40
65
|
end
|
66
|
+
def count; @_table.items.count; end
|
41
67
|
|
42
68
|
def is_new?; @is_new; end
|
43
69
|
def save(hash_value = nil)
|
44
70
|
raise "missing hash value" if hash_value.nil? and is_new?
|
45
|
-
|
71
|
+
if is_new?
|
72
|
+
if @range_key
|
73
|
+
values = { @hash_key.to_sym => hash_value, @range_key.to_sym => @attributes[@range_key] }
|
74
|
+
else
|
75
|
+
values = { @hash_key.to_sym => hash_value }
|
76
|
+
end
|
77
|
+
options = {}
|
78
|
+
options[:unless_exists] = @hash_key if hash_key_conditional_check
|
79
|
+
@_item = @_table.items.create(values, options)
|
80
|
+
end
|
46
81
|
if @_item
|
47
|
-
@
|
82
|
+
if @range_key
|
83
|
+
attrs = @attributes; attrs.delete(@range_key)
|
84
|
+
@_item.attributes.set(attrs)
|
85
|
+
else
|
86
|
+
@_item.attributes.set(@attributes)
|
87
|
+
end
|
48
88
|
@hash_value = hash_value if is_new?
|
49
89
|
@is_new = false
|
50
90
|
@_item.attributes.to_h
|
51
91
|
end
|
52
92
|
end
|
53
93
|
|
94
|
+
def scan(attrs, limit = nil)
|
95
|
+
values = []; options = {}
|
96
|
+
options[:limit] = limit if limit
|
97
|
+
@_table.items.where(attrs).each(options) do |item|
|
98
|
+
values << item.attributes.to_h
|
99
|
+
end
|
100
|
+
values
|
101
|
+
end
|
102
|
+
|
54
103
|
def destroy
|
55
104
|
unless is_new?
|
56
105
|
@_item.delete
|
data/test/helper.rb
CHANGED
@@ -14,8 +14,12 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
14
14
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
15
|
require 'rdy'
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
RDY_SIMPLE_TABLE = 'rdy_test_simple'
|
18
|
+
RDY_RANGE_TABLE = 'rdy_test_range'
|
19
|
+
|
20
|
+
puts "Setting up test tables for Rdy..."
|
21
|
+
Rdy.create_table(RDY_SIMPLE_TABLE, 10, 5, :id => :string) rescue nil
|
22
|
+
Rdy.create_table(RDY_RANGE_TABLE, 10, 5, {:id => :string}, {:foo => :string}) rescue nil
|
19
23
|
|
20
24
|
class Test::Unit::TestCase
|
21
25
|
end
|
data/test/test_rdy.rb
CHANGED
@@ -2,16 +2,26 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestRdy < Test::Unit::TestCase
|
4
4
|
def rdy
|
5
|
-
@rdy = Rdy.new(
|
5
|
+
@rdy = Rdy.new(RDY_SIMPLE_TABLE, [:id, :string])
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
|
+
def rdy_range
|
9
|
+
@rdy2 = Rdy.new(RDY_RANGE_TABLE, [:id, :string], [:foo, :string])
|
10
|
+
end
|
11
|
+
|
8
12
|
should "create an Rdy instance" do
|
9
|
-
rdy
|
10
|
-
assert_equal rdy.table, 'rdy_test'
|
13
|
+
assert_equal rdy.table, RDY_SIMPLE_TABLE
|
11
14
|
assert_equal rdy.hash_key, 'id'
|
12
15
|
assert_nil rdy.hash_value, nil
|
13
16
|
end
|
14
17
|
|
18
|
+
should "create an Rdy instance for table with a range key" do
|
19
|
+
assert_equal rdy_range.table, RDY_RANGE_TABLE
|
20
|
+
assert_equal rdy_range.hash_key, 'id'
|
21
|
+
assert_equal rdy_range.range_key, 'foo'
|
22
|
+
assert_nil rdy.hash_value, nil
|
23
|
+
end
|
24
|
+
|
15
25
|
context "Attributes" do
|
16
26
|
setup do
|
17
27
|
rdy
|
@@ -20,10 +30,11 @@ class TestRdy < Test::Unit::TestCase
|
|
20
30
|
should "create assign a value to an attribute" do
|
21
31
|
@rdy.foo = "bar"
|
22
32
|
@rdy.bar = 23
|
23
|
-
@rdy.
|
33
|
+
@rdy.tags = ['a', 'b', 'c']
|
24
34
|
assert_equal @rdy.foo, "bar"
|
25
35
|
assert_equal @rdy.bar, 23
|
26
|
-
|
36
|
+
assert_not_nil @rdy.tags
|
37
|
+
assert_equal @rdy.tags.size, 3
|
27
38
|
end
|
28
39
|
|
29
40
|
should "have values in attributes hash" do
|
@@ -43,14 +54,18 @@ class TestRdy < Test::Unit::TestCase
|
|
43
54
|
should "save an item" do
|
44
55
|
assert @rdy.is_new?
|
45
56
|
@rdy.foo = "bar"
|
57
|
+
@rdy.count = 1
|
58
|
+
@rdy.tags = ['a', 'b', 'c']
|
46
59
|
assert_equal @rdy.foo, "bar"
|
47
|
-
assert_equal @rdy.table,
|
60
|
+
assert_equal @rdy.table, RDY_SIMPLE_TABLE
|
48
61
|
assert_nil @rdy.hash_value
|
49
62
|
attributes = @rdy.save('1')
|
50
63
|
assert_equal @rdy.hash_value, '1'
|
51
|
-
assert_equal attributes.size,
|
64
|
+
assert_equal attributes.size, 4
|
52
65
|
assert attributes.keys.include?('id')
|
53
66
|
assert attributes.keys.include?('foo')
|
67
|
+
assert attributes.keys.include?('count')
|
68
|
+
assert attributes.keys.include?('tags')
|
54
69
|
assert !@rdy.is_new?
|
55
70
|
@rdy.destroy
|
56
71
|
end
|
@@ -77,7 +92,7 @@ class TestRdy < Test::Unit::TestCase
|
|
77
92
|
@rdy.destroy
|
78
93
|
end
|
79
94
|
end
|
80
|
-
|
95
|
+
|
81
96
|
context "Destroying" do
|
82
97
|
setup do
|
83
98
|
rdy
|
@@ -94,4 +109,77 @@ class TestRdy < Test::Unit::TestCase
|
|
94
109
|
assert_nil @rdy.hash_value
|
95
110
|
end
|
96
111
|
end
|
112
|
+
|
113
|
+
context "Scan Table" do
|
114
|
+
setup do
|
115
|
+
rdy_range
|
116
|
+
end
|
117
|
+
|
118
|
+
should "scan table by attribute values" do
|
119
|
+
@rdy2 = Rdy.new(RDY_RANGE_TABLE, [:id, :string], [:foo, :string])
|
120
|
+
@rdy2.foo = "bar1"
|
121
|
+
@rdy2.data = "test"
|
122
|
+
@rdy2.save("4")
|
123
|
+
|
124
|
+
@rdy3 = Rdy.new(RDY_RANGE_TABLE, [:id, :string], [:foo, :string])
|
125
|
+
@rdy3.foo = "bar2"
|
126
|
+
@rdy3.data = "test"
|
127
|
+
@rdy3.save("5")
|
128
|
+
|
129
|
+
attrs = @rdy2.scan(:data => 'test')
|
130
|
+
assert_not_nil attrs
|
131
|
+
assert_equal attrs.size, 2
|
132
|
+
|
133
|
+
attrs = @rdy2.scan(:data => 'test', :foo => 'bar1')
|
134
|
+
assert_not_nil attrs
|
135
|
+
assert_equal attrs.size, 1
|
136
|
+
assert_equal attrs[0]['foo'], 'bar1'
|
137
|
+
|
138
|
+
attrs = @rdy2.scan(:data => 'test', :foo => 'bar2')
|
139
|
+
assert_not_nil attrs
|
140
|
+
assert_equal attrs.size, 1
|
141
|
+
assert_equal attrs[0]['foo'], 'bar2'
|
142
|
+
|
143
|
+
limit = 1
|
144
|
+
attrs = @rdy2.scan({:data => 'test'}, limit)
|
145
|
+
assert_not_nil attrs
|
146
|
+
assert_equal attrs.size, 1
|
147
|
+
|
148
|
+
@rdy2.find("4", "bar1")
|
149
|
+
@rdy2.destroy
|
150
|
+
|
151
|
+
@rdy3.find("5", "bar2")
|
152
|
+
@rdy3.destroy
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context "Finding hash-key based items" do
|
157
|
+
setup do
|
158
|
+
rdy
|
159
|
+
end
|
160
|
+
|
161
|
+
should "find an item by hash-key" do
|
162
|
+
@rdy.foo = "bar"
|
163
|
+
attributes = @rdy.save("6")
|
164
|
+
@rdy.find("6")
|
165
|
+
assert_not_nil @rdy.foo
|
166
|
+
assert_equal @rdy.foo, "bar"
|
167
|
+
@rdy.destroy
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context "Finding hash-key/range based items" do
|
172
|
+
setup do
|
173
|
+
rdy_range
|
174
|
+
end
|
175
|
+
|
176
|
+
should "find an item by hash-key & range combination" do
|
177
|
+
@rdy2.foo = "bar"
|
178
|
+
attributes = @rdy2.save("7")
|
179
|
+
@rdy2.find("7", "bar")
|
180
|
+
assert_not_nil @rdy2.foo
|
181
|
+
assert_equal @rdy2.foo, "bar"
|
182
|
+
@rdy2.destroy
|
183
|
+
end
|
184
|
+
end
|
97
185
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Oliver Kiessler
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-02-
|
18
|
+
date: 2012-02-15 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|