adapter 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Guardfile +3 -3
- data/README.md +2 -2
- data/examples/overriding_serialization.rb +4 -4
- data/lib/adapter.rb +6 -6
- data/lib/adapter/defaults.rb +10 -7
- data/lib/adapter/memory.rb +2 -2
- data/lib/adapter/spec/an_adapter.rb +95 -53
- data/lib/adapter/version.rb +1 -1
- data/spec/helper.rb +2 -2
- metadata +4 -5
- data/Gemfile.lock +0 -39
data/.gitignore
CHANGED
data/Guardfile
CHANGED
@@ -4,9 +4,9 @@ rspec_options = {
|
|
4
4
|
}
|
5
5
|
|
6
6
|
guard 'rspec', rspec_options do
|
7
|
-
watch(%r{^spec/.+_spec\.rb$})
|
8
|
-
watch(%r{^lib/(.+)\.rb$})
|
9
|
-
watch('spec/helper.rb')
|
7
|
+
watch(%r{^spec/.+_spec\.rb$}) { "spec" }
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) { "spec" }
|
9
|
+
watch('spec/helper.rb') { "spec" }
|
10
10
|
watch(%r{lib/adapter/spec/(.+)\.rb}) { "spec" }
|
11
11
|
end
|
12
12
|
|
data/README.md
CHANGED
@@ -19,12 +19,12 @@ require 'adapter/memory'
|
|
19
19
|
# Marshal.load/dump. Also, important to note that this does not affect
|
20
20
|
# the memory adapter which still uses Marshal.
|
21
21
|
Adapter.define(:memory_json, Adapter::Memory) do
|
22
|
-
def encode(
|
23
|
-
ActiveSupport::JSON.encode(
|
22
|
+
def encode(attributes)
|
23
|
+
ActiveSupport::JSON.encode(attributes)
|
24
24
|
end
|
25
25
|
|
26
|
-
def decode(
|
27
|
-
ActiveSupport::JSON.decode(
|
26
|
+
def decode(attributes)
|
27
|
+
ActiveSupport::JSON.decode(attributes)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
data/lib/adapter.rb
CHANGED
@@ -3,15 +3,15 @@ require 'adapter/defaults'
|
|
3
3
|
require 'adapter/exceptions'
|
4
4
|
|
5
5
|
module Adapter
|
6
|
+
extend self
|
6
7
|
extend Asserts
|
7
|
-
include Defaults
|
8
8
|
|
9
9
|
# Stores the definitions for each adapter by name
|
10
|
-
def
|
10
|
+
def definitions
|
11
11
|
@definitions ||= {}
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def define(name, mod=nil, &block)
|
15
15
|
definition_module = Module.new
|
16
16
|
definition_module.send(:include, Defaults)
|
17
17
|
definition_module.send(:include, mod) unless mod.nil?
|
@@ -22,17 +22,17 @@ module Adapter
|
|
22
22
|
end
|
23
23
|
|
24
24
|
# Memoizes adapter instances based on their definitions
|
25
|
-
def
|
25
|
+
def adapters
|
26
26
|
@adapters ||= {}
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def [](name)
|
30
30
|
assert_valid_adapter(name)
|
31
31
|
adapters[name.to_sym] ||= get_adapter_instance(name)
|
32
32
|
end
|
33
33
|
|
34
34
|
private
|
35
|
-
def
|
35
|
+
def get_adapter_instance(name)
|
36
36
|
Class.new do
|
37
37
|
attr_reader :client, :options
|
38
38
|
|
data/lib/adapter/defaults.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
module Adapter
|
2
2
|
module Defaults
|
3
|
-
def fetch(key,
|
3
|
+
def fetch(key, default_attributes=nil)
|
4
4
|
read(key) || begin
|
5
|
-
|
6
|
-
|
5
|
+
if block_given?
|
6
|
+
yield(key)
|
7
|
+
else
|
8
|
+
default_attributes
|
9
|
+
end
|
7
10
|
end
|
8
11
|
end
|
9
12
|
|
@@ -21,12 +24,12 @@ module Adapter
|
|
21
24
|
key
|
22
25
|
end
|
23
26
|
|
24
|
-
def encode(
|
25
|
-
|
27
|
+
def encode(attributes)
|
28
|
+
attributes
|
26
29
|
end
|
27
30
|
|
28
|
-
def decode(
|
29
|
-
|
31
|
+
def decode(attributes)
|
32
|
+
attributes
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
data/lib/adapter/memory.rb
CHANGED
@@ -25,9 +25,12 @@ shared_examples_for "an adapter" do
|
|
25
25
|
adapter.read(key).should be_nil
|
26
26
|
end
|
27
27
|
|
28
|
-
it "returns
|
28
|
+
it "returns attributes if key available" do
|
29
29
|
adapter.write(key, attributes)
|
30
|
-
adapter.read(key)
|
30
|
+
result = adapter.read(key)
|
31
|
+
attributes.each do |column, value|
|
32
|
+
result[column].should eq(value)
|
33
|
+
end
|
31
34
|
end
|
32
35
|
end
|
33
36
|
|
@@ -36,9 +39,12 @@ shared_examples_for "an adapter" do
|
|
36
39
|
adapter.get(key).should be_nil
|
37
40
|
end
|
38
41
|
|
39
|
-
it "returns
|
42
|
+
it "returns attributes if key available" do
|
40
43
|
adapter.write(key, attributes)
|
41
|
-
adapter.get(key)
|
44
|
+
result = adapter.get(key)
|
45
|
+
attributes.each do |column, value|
|
46
|
+
result[column].should eq(value)
|
47
|
+
end
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
@@ -47,9 +53,12 @@ shared_examples_for "an adapter" do
|
|
47
53
|
adapter[key].should be_nil
|
48
54
|
end
|
49
55
|
|
50
|
-
it "returns
|
56
|
+
it "returns attributes if key available" do
|
51
57
|
adapter.write(key, attributes)
|
52
|
-
adapter[key]
|
58
|
+
result = adapter[key]
|
59
|
+
attributes.each do |column, value|
|
60
|
+
result[column].should eq(value)
|
61
|
+
end
|
53
62
|
end
|
54
63
|
end
|
55
64
|
|
@@ -59,21 +68,32 @@ shared_examples_for "an adapter" do
|
|
59
68
|
adapter.write(key2, attributes2)
|
60
69
|
end
|
61
70
|
|
62
|
-
it "returns Hash of keys and
|
63
|
-
adapter.read_multiple(key, key2)
|
64
|
-
|
65
|
-
|
66
|
-
|
71
|
+
it "returns Hash of keys and attributes" do
|
72
|
+
result = adapter.read_multiple(key, key2)
|
73
|
+
|
74
|
+
attributes.each do |column, value|
|
75
|
+
result[key][column].should eq(value)
|
76
|
+
end
|
77
|
+
|
78
|
+
attributes2.each do |column, value|
|
79
|
+
result[key2][column].should eq(value)
|
80
|
+
end
|
67
81
|
end
|
68
82
|
|
69
83
|
context "with mix of keys that are and are not available" do
|
70
|
-
it "returns Hash of keys and
|
71
|
-
adapter.read_multiple(key, key2, 'foo', 'bar')
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
84
|
+
it "returns Hash of keys and attributes where unavailable keys are nil" do
|
85
|
+
result = adapter.read_multiple(key, key2, 'foo', 'bar')
|
86
|
+
|
87
|
+
attributes.each do |column, value|
|
88
|
+
result[key][column].should eq(value)
|
89
|
+
end
|
90
|
+
|
91
|
+
attributes2.each do |column, value|
|
92
|
+
result[key2][column].should eq(value)
|
93
|
+
end
|
94
|
+
|
95
|
+
result['foo'].should be_nil
|
96
|
+
result['bar'].should be_nil
|
77
97
|
end
|
78
98
|
end
|
79
99
|
end
|
@@ -84,21 +104,32 @@ shared_examples_for "an adapter" do
|
|
84
104
|
adapter.write(key2, attributes2)
|
85
105
|
end
|
86
106
|
|
87
|
-
it "returns Hash of keys and
|
88
|
-
adapter.get_multiple(key, key2)
|
89
|
-
|
90
|
-
|
91
|
-
|
107
|
+
it "returns Hash of keys and attributes" do
|
108
|
+
result = adapter.get_multiple(key, key2)
|
109
|
+
|
110
|
+
attributes.each do |column, value|
|
111
|
+
result[key][column].should eq(value)
|
112
|
+
end
|
113
|
+
|
114
|
+
attributes2.each do |column, value|
|
115
|
+
result[key2][column].should eq(value)
|
116
|
+
end
|
92
117
|
end
|
93
118
|
|
94
119
|
context "with mix of keys that are and are not available" do
|
95
|
-
it "returns Hash of keys and
|
96
|
-
adapter.get_multiple(key, key2, 'foo', 'bar')
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
120
|
+
it "returns Hash of keys and attributes where unavailable keys are nil" do
|
121
|
+
result = adapter.get_multiple(key, key2, 'foo', 'bar')
|
122
|
+
|
123
|
+
attributes.each do |column, value|
|
124
|
+
result[key][column].should eq(value)
|
125
|
+
end
|
126
|
+
|
127
|
+
attributes2.each do |column, value|
|
128
|
+
result[key2][column].should eq(value)
|
129
|
+
end
|
130
|
+
|
131
|
+
result['foo'].should be_nil
|
132
|
+
result['bar'].should be_nil
|
102
133
|
end
|
103
134
|
end
|
104
135
|
end
|
@@ -115,9 +146,9 @@ shared_examples_for "an adapter" do
|
|
115
146
|
end
|
116
147
|
|
117
148
|
describe "#fetch" do
|
118
|
-
context "with key not
|
119
|
-
context "with default
|
120
|
-
it "returns default
|
149
|
+
context "with key not available" do
|
150
|
+
context "with default attributes" do
|
151
|
+
it "returns default" do
|
121
152
|
adapter.fetch(key, {}).should eq({})
|
122
153
|
end
|
123
154
|
end
|
@@ -129,11 +160,14 @@ shared_examples_for "an adapter" do
|
|
129
160
|
end
|
130
161
|
end
|
131
162
|
|
132
|
-
context "with key that is
|
133
|
-
context "with default
|
134
|
-
it "returns
|
163
|
+
context "with key that is available" do
|
164
|
+
context "with default attributes" do
|
165
|
+
it "returns result of read instead of default" do
|
135
166
|
adapter.write(key, attributes2)
|
136
|
-
adapter.fetch(key, attributes)
|
167
|
+
result = adapter.fetch(key, attributes)
|
168
|
+
attributes2.each do |column, value|
|
169
|
+
result[column].should eq(value)
|
170
|
+
end
|
137
171
|
end
|
138
172
|
end
|
139
173
|
|
@@ -149,53 +183,61 @@ shared_examples_for "an adapter" do
|
|
149
183
|
end
|
150
184
|
|
151
185
|
describe "#write" do
|
152
|
-
it "sets key to
|
186
|
+
it "sets key to attributes" do
|
153
187
|
adapter.write(key, attributes)
|
154
|
-
adapter.read(key)
|
188
|
+
result = adapter.read(key)
|
189
|
+
attributes.each do |column, value|
|
190
|
+
result[column].should eq(value)
|
191
|
+
end
|
155
192
|
end
|
156
193
|
end
|
157
194
|
|
158
195
|
describe "#set" do
|
159
|
-
it "sets key to
|
196
|
+
it "sets key to attributes" do
|
160
197
|
adapter.set(key, attributes)
|
161
|
-
adapter.read(key)
|
198
|
+
result = adapter.read(key)
|
199
|
+
attributes.each do |column, value|
|
200
|
+
result[column].should eq(value)
|
201
|
+
end
|
162
202
|
end
|
163
203
|
end
|
164
204
|
|
165
205
|
describe "#[]=" do
|
166
|
-
it "sets key to
|
206
|
+
it "sets key to attributes" do
|
167
207
|
adapter[key] = attributes
|
168
|
-
adapter.read(key)
|
208
|
+
result = adapter.read(key)
|
209
|
+
attributes.each do |column, value|
|
210
|
+
result[column].should eq(value)
|
211
|
+
end
|
169
212
|
end
|
170
213
|
end
|
171
214
|
|
172
215
|
describe "#delete" do
|
173
216
|
context "when key available" do
|
174
|
-
|
217
|
+
it "removes key" do
|
175
218
|
adapter.write(key, attributes)
|
219
|
+
adapter.key?(key).should be_true
|
176
220
|
adapter.delete(key)
|
177
|
-
end
|
178
|
-
|
179
|
-
it "removes key" do
|
180
221
|
adapter.key?(key).should be_false
|
181
222
|
end
|
182
223
|
end
|
183
224
|
|
184
225
|
context "when key not available" do
|
185
226
|
it "does not complain" do
|
227
|
+
adapter.key?(key).should be_false
|
186
228
|
adapter.delete(key)
|
229
|
+
adapter.key?(key).should be_false
|
187
230
|
end
|
188
231
|
end
|
189
232
|
end
|
190
233
|
|
191
234
|
describe "#clear" do
|
192
|
-
|
193
|
-
adapter
|
194
|
-
adapter
|
235
|
+
it "removes all available keys" do
|
236
|
+
adapter.write(key, attributes)
|
237
|
+
adapter.write(key2, attributes2)
|
238
|
+
adapter.key?(key).should be_true
|
239
|
+
adapter.key?(key2).should be_true
|
195
240
|
adapter.clear
|
196
|
-
end
|
197
|
-
|
198
|
-
it "removes all stored keys" do
|
199
241
|
adapter.key?(key).should be_false
|
200
242
|
adapter.key?(key2).should be_false
|
201
243
|
end
|
data/lib/adapter/version.rb
CHANGED
data/spec/helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-11-
|
14
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
15
15
|
dependencies: []
|
16
16
|
description:
|
17
17
|
email:
|
@@ -26,7 +26,6 @@ files:
|
|
26
26
|
- .travis.yml
|
27
27
|
- Changelog.md
|
28
28
|
- Gemfile
|
29
|
-
- Gemfile.lock
|
30
29
|
- Guardfile
|
31
30
|
- LICENSE
|
32
31
|
- README.md
|
@@ -61,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
60
|
version: '0'
|
62
61
|
segments:
|
63
62
|
- 0
|
64
|
-
hash: -
|
63
|
+
hash: -3288681116228721407
|
65
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
65
|
none: false
|
67
66
|
requirements:
|
@@ -70,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
69
|
version: '0'
|
71
70
|
segments:
|
72
71
|
- 0
|
73
|
-
hash: -
|
72
|
+
hash: -3288681116228721407
|
74
73
|
requirements: []
|
75
74
|
rubyforge_project:
|
76
75
|
rubygems_version: 1.8.23
|
data/Gemfile.lock
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
adapter (0.6.1)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: http://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.1.3)
|
10
|
-
ffi (1.0.11)
|
11
|
-
guard (1.0.0)
|
12
|
-
ffi (>= 0.5.0)
|
13
|
-
thor (~> 0.14.6)
|
14
|
-
guard-bundler (0.1.3)
|
15
|
-
bundler (>= 1.0.0)
|
16
|
-
guard (>= 0.2.2)
|
17
|
-
guard-rspec (0.6.0)
|
18
|
-
guard (>= 0.10.0)
|
19
|
-
rake (0.9.2)
|
20
|
-
rspec (2.5.0)
|
21
|
-
rspec-core (~> 2.5.0)
|
22
|
-
rspec-expectations (~> 2.5.0)
|
23
|
-
rspec-mocks (~> 2.5.0)
|
24
|
-
rspec-core (2.5.1)
|
25
|
-
rspec-expectations (2.5.0)
|
26
|
-
diff-lcs (~> 1.1.2)
|
27
|
-
rspec-mocks (2.5.0)
|
28
|
-
thor (0.14.6)
|
29
|
-
|
30
|
-
PLATFORMS
|
31
|
-
ruby
|
32
|
-
|
33
|
-
DEPENDENCIES
|
34
|
-
adapter!
|
35
|
-
guard
|
36
|
-
guard-bundler
|
37
|
-
guard-rspec
|
38
|
-
rake
|
39
|
-
rspec
|