adapter 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +14 -6
- data/README.md +7 -9
- data/lib/adapter.rb +0 -8
- data/lib/adapter/defaults.rb +6 -18
- data/lib/adapter/memory.rb +7 -7
- data/lib/adapter/spec/an_adapter.rb +38 -79
- data/lib/adapter/version.rb +1 -1
- data/spec/adapter_spec.rb +2 -60
- data/spec/support/module_helpers.rb +7 -7
- metadata +4 -7
- data/examples/overriding_serialization.rb +0 -41
- data/spec/adapter/defaults_spec.rb +0 -33
data/Changelog.md
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
-
|
1
|
+
# Changelog
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
## 0.7.0
|
4
|
+
|
5
|
+
* `key_for`, `encode` and `decode` are no longer provided or necessary.
|
6
|
+
* All methods must accept options as last parameter. They do not need to do anything with them, but they must at least be declared.
|
7
|
+
* Tightened up API. `get`, `set`, `[]`, `[]=`, and `get_multiple` are now gone.
|
8
|
+
|
9
|
+
## 0.6.0
|
10
|
+
|
11
|
+
* Now aimed at key/value where value is Hash of attributes instead of just any random value, such as String, Array, etc.
|
12
|
+
* `#key_for` now defaults to whatever was passed in, rather than Marshaling anything that was not a String or a Symbol.
|
13
|
+
* `#encode` and `#decode` now default to whatever was passed in, instead of marshaling.
|
14
|
+
* `#delete` no longer returns value. Raise error if delete fails. If you need value, do a read before delete.
|
15
|
+
* Added `#read_multiple`. This can be overridden per adapter to make it more efficient based on the data store. Defaults to multiple single reads.
|
data/README.md
CHANGED
@@ -8,19 +8,19 @@ An adapter requires 4 methods to work: read, write, delete and clear.
|
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
Adapter.define(:memory) do
|
11
|
-
def read(key)
|
12
|
-
|
11
|
+
def read(key, options = nil)
|
12
|
+
client[key]
|
13
13
|
end
|
14
14
|
|
15
|
-
def write(key, attributes)
|
16
|
-
client[
|
15
|
+
def write(key, attributes, options = nil)
|
16
|
+
client[key] = attributes
|
17
17
|
end
|
18
18
|
|
19
|
-
def delete(key)
|
20
|
-
client.delete(
|
19
|
+
def delete(key, options = nil)
|
20
|
+
client.delete(key)
|
21
21
|
end
|
22
22
|
|
23
|
-
def clear
|
23
|
+
def clear(options = nil)
|
24
24
|
client.clear
|
25
25
|
end
|
26
26
|
end
|
@@ -44,8 +44,6 @@ adapter.delete('foo')
|
|
44
44
|
adapter.fetch('foo', 'bar') # returns bar and sets foo to bar
|
45
45
|
```
|
46
46
|
|
47
|
-
`get` and `[]` are aliased to read. `set` and `[]=` are aliased to write.
|
48
|
-
|
49
47
|
Note: You can also optionally provide a lock method. [Read More](https://github.com/jnunemaker/adapter/wiki/Locking)
|
50
48
|
|
51
49
|
## Adapter Power User Guides
|
data/lib/adapter.rb
CHANGED
@@ -43,14 +43,6 @@ module Adapter
|
|
43
43
|
|
44
44
|
include Adapter.definitions[name.to_sym]
|
45
45
|
|
46
|
-
alias_method :get, :read
|
47
|
-
alias_method :set, :write
|
48
|
-
|
49
|
-
alias_method :[], :read
|
50
|
-
alias_method :[]=, :write
|
51
|
-
|
52
|
-
alias_method :get_multiple, :read_multiple
|
53
|
-
|
54
46
|
def eql?(other)
|
55
47
|
self.class.eql?(other.class) && client == other.client
|
56
48
|
end
|
data/lib/adapter/defaults.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Adapter
|
2
2
|
module Defaults
|
3
|
-
def fetch(key, default_attributes=nil)
|
4
|
-
read(key) || begin
|
3
|
+
def fetch(key, default_attributes=nil, options = nil)
|
4
|
+
read(key, options) || begin
|
5
5
|
if block_given?
|
6
6
|
yield(key)
|
7
7
|
else
|
@@ -10,26 +10,14 @@ module Adapter
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def read_multiple(
|
13
|
+
def read_multiple(keys, options = nil)
|
14
14
|
result = {}
|
15
|
-
keys.each { |key| result[
|
15
|
+
keys.each { |key| result[key] = read(key, options) }
|
16
16
|
result
|
17
17
|
end
|
18
18
|
|
19
|
-
def key?(key)
|
20
|
-
!read(key).nil?
|
21
|
-
end
|
22
|
-
|
23
|
-
def key_for(key)
|
24
|
-
key
|
25
|
-
end
|
26
|
-
|
27
|
-
def encode(attributes)
|
28
|
-
attributes
|
29
|
-
end
|
30
|
-
|
31
|
-
def decode(attributes)
|
32
|
-
attributes
|
19
|
+
def key?(key, options = nil)
|
20
|
+
!read(key, options).nil?
|
33
21
|
end
|
34
22
|
end
|
35
23
|
end
|
data/lib/adapter/memory.rb
CHANGED
@@ -2,19 +2,19 @@ require 'adapter'
|
|
2
2
|
|
3
3
|
module Adapter
|
4
4
|
module Memory
|
5
|
-
def read(key)
|
6
|
-
|
5
|
+
def read(key, options = nil)
|
6
|
+
client[key]
|
7
7
|
end
|
8
8
|
|
9
|
-
def write(key, attributes)
|
10
|
-
client[
|
9
|
+
def write(key, attributes, options = nil)
|
10
|
+
client[key] = attributes
|
11
11
|
end
|
12
12
|
|
13
|
-
def delete(key)
|
14
|
-
|
13
|
+
def delete(key, options = nil)
|
14
|
+
client.delete(key)
|
15
15
|
end
|
16
16
|
|
17
|
-
def clear
|
17
|
+
def clear(options = nil)
|
18
18
|
client.clear
|
19
19
|
end
|
20
20
|
end
|
@@ -34,33 +34,11 @@ shared_examples_for "an adapter" do
|
|
34
34
|
result[column].should eq(value)
|
35
35
|
end
|
36
36
|
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "#get" do
|
40
|
-
it "returns nil if key not available" do
|
41
|
-
adapter.get(key).should be_nil
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns attributes if key available" do
|
45
|
-
adapter.write(key, attributes)
|
46
|
-
result = adapter.get(key)
|
47
|
-
attributes.each do |column, value|
|
48
|
-
result[column].should eq(value)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
37
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
it "returns attributes if key available" do
|
59
|
-
adapter.write(key, attributes)
|
60
|
-
result = adapter[key]
|
61
|
-
attributes.each do |column, value|
|
62
|
-
result[column].should eq(value)
|
63
|
-
end
|
38
|
+
it "accepts options" do
|
39
|
+
expect {
|
40
|
+
adapter.read(key, :something => 'else')
|
41
|
+
}.to_not raise_error
|
64
42
|
end
|
65
43
|
end
|
66
44
|
|
@@ -71,7 +49,7 @@ shared_examples_for "an adapter" do
|
|
71
49
|
end
|
72
50
|
|
73
51
|
it "returns Hash of keys and attributes" do
|
74
|
-
result = adapter.read_multiple(key, key2)
|
52
|
+
result = adapter.read_multiple([key, key2])
|
75
53
|
|
76
54
|
attributes.each do |column, value|
|
77
55
|
result[key][column].should eq(value)
|
@@ -84,7 +62,7 @@ shared_examples_for "an adapter" do
|
|
84
62
|
|
85
63
|
context "with mix of keys that are and are not available" do
|
86
64
|
it "returns Hash of keys and attributes where unavailable keys are nil" do
|
87
|
-
result = adapter.read_multiple(key, key2, unavailable_key)
|
65
|
+
result = adapter.read_multiple([key, key2, unavailable_key])
|
88
66
|
|
89
67
|
attributes.each do |column, value|
|
90
68
|
result[key][column].should eq(value)
|
@@ -97,40 +75,11 @@ shared_examples_for "an adapter" do
|
|
97
75
|
result[unavailable_key].should be_nil
|
98
76
|
end
|
99
77
|
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "#get_multiple" do
|
103
|
-
before do
|
104
|
-
adapter.write(key, attributes)
|
105
|
-
adapter.write(key2, attributes2)
|
106
|
-
end
|
107
|
-
|
108
|
-
it "returns Hash of keys and attributes" do
|
109
|
-
result = adapter.get_multiple(key, key2)
|
110
|
-
|
111
|
-
attributes.each do |column, value|
|
112
|
-
result[key][column].should eq(value)
|
113
|
-
end
|
114
|
-
|
115
|
-
attributes2.each do |column, value|
|
116
|
-
result[key2][column].should eq(value)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "with mix of keys that are and are not available" do
|
121
|
-
it "returns Hash of keys and attributes where unavailable keys are nil" do
|
122
|
-
result = adapter.get_multiple(key, key2, unavailable_key)
|
123
|
-
|
124
|
-
attributes.each do |column, value|
|
125
|
-
result[key][column].should eq(value)
|
126
|
-
end
|
127
|
-
|
128
|
-
attributes2.each do |column, value|
|
129
|
-
result[key2][column].should eq(value)
|
130
|
-
end
|
131
78
|
|
132
|
-
|
133
|
-
|
79
|
+
it "accepts options" do
|
80
|
+
expect {
|
81
|
+
adapter.read_multiple([key], :something => 'else')
|
82
|
+
}.to_not raise_error
|
134
83
|
end
|
135
84
|
end
|
136
85
|
|
@@ -143,6 +92,12 @@ shared_examples_for "an adapter" do
|
|
143
92
|
it "returns false if key not available" do
|
144
93
|
adapter.key?(key).should be_false
|
145
94
|
end
|
95
|
+
|
96
|
+
it "accepts options" do
|
97
|
+
expect {
|
98
|
+
adapter.key?(key, :something => 'else')
|
99
|
+
}.to_not raise_error
|
100
|
+
end
|
146
101
|
end
|
147
102
|
|
148
103
|
describe "#fetch" do
|
@@ -180,35 +135,27 @@ shared_examples_for "an adapter" do
|
|
180
135
|
end
|
181
136
|
end
|
182
137
|
end
|
183
|
-
end
|
184
138
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
attributes.each do |column, value|
|
190
|
-
result[column].should eq(value)
|
191
|
-
end
|
139
|
+
it "accepts options" do
|
140
|
+
expect {
|
141
|
+
adapter.fetch(key, true, :something => 'else')
|
142
|
+
}.to_not raise_error
|
192
143
|
end
|
193
144
|
end
|
194
145
|
|
195
|
-
describe "#
|
146
|
+
describe "#write" do
|
196
147
|
it "sets key to attributes" do
|
197
|
-
adapter.
|
148
|
+
adapter.write(key, attributes)
|
198
149
|
result = adapter.read(key)
|
199
150
|
attributes.each do |column, value|
|
200
151
|
result[column].should eq(value)
|
201
152
|
end
|
202
153
|
end
|
203
|
-
end
|
204
154
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
attributes.each do |column, value|
|
210
|
-
result[column].should eq(value)
|
211
|
-
end
|
155
|
+
it "accepts options" do
|
156
|
+
expect {
|
157
|
+
adapter.write(key, attributes, :something => 'else')
|
158
|
+
}.to_not raise_error
|
212
159
|
end
|
213
160
|
end
|
214
161
|
|
@@ -229,6 +176,12 @@ shared_examples_for "an adapter" do
|
|
229
176
|
adapter.key?(key).should be_false
|
230
177
|
end
|
231
178
|
end
|
179
|
+
|
180
|
+
it "accepts options" do
|
181
|
+
expect {
|
182
|
+
adapter.delete(key, :something => 'else')
|
183
|
+
}.to_not raise_error
|
184
|
+
end
|
232
185
|
end
|
233
186
|
|
234
187
|
describe "#clear" do
|
@@ -241,5 +194,11 @@ shared_examples_for "an adapter" do
|
|
241
194
|
adapter.key?(key).should be_false
|
242
195
|
adapter.key?(key2).should be_false
|
243
196
|
end
|
197
|
+
|
198
|
+
it "accepts options" do
|
199
|
+
expect {
|
200
|
+
adapter.clear(:something => 'else')
|
201
|
+
}.to_not raise_error
|
202
|
+
end
|
244
203
|
end
|
245
204
|
end
|
data/lib/adapter/version.rb
CHANGED
data/spec/adapter_spec.rb
CHANGED
@@ -43,9 +43,8 @@ describe Adapter do
|
|
43
43
|
include Adapter.definitions[:memory]
|
44
44
|
end.tap do |klass|
|
45
45
|
klass.new.respond_to?(:fetch).should be_true
|
46
|
-
klass.new.respond_to?(:
|
47
|
-
klass.new.respond_to?(:
|
48
|
-
klass.new.respond_to?(:decode, true).should be_true
|
46
|
+
klass.new.respond_to?(:key?).should be_true
|
47
|
+
klass.new.respond_to?(:read_multiple).should be_true
|
49
48
|
end
|
50
49
|
end
|
51
50
|
|
@@ -110,33 +109,6 @@ describe Adapter do
|
|
110
109
|
end
|
111
110
|
end
|
112
111
|
|
113
|
-
describe "Overriding encode/decode" do
|
114
|
-
before do
|
115
|
-
Adapter.define(:memory_json, valid_module) do
|
116
|
-
def encode(value)
|
117
|
-
'encoded'
|
118
|
-
end
|
119
|
-
|
120
|
-
def decode(value)
|
121
|
-
'decoded'
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
let(:adapter) { Adapter[:memory_json].new({}) }
|
126
|
-
|
127
|
-
it "encodes correctly" do
|
128
|
-
hash = {'foo' => 'bar'}
|
129
|
-
adapter.write('foo', hash)
|
130
|
-
adapter.client['foo'].should == 'encoded'
|
131
|
-
end
|
132
|
-
|
133
|
-
it "decodes correctly" do
|
134
|
-
hash = {'foo' => 'bar'}
|
135
|
-
adapter.client['foo'] = hash
|
136
|
-
adapter.read('foo').should == 'decoded'
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
112
|
describe "Redefining an adapter" do
|
141
113
|
before do
|
142
114
|
Adapter.define(:memory, valid_module)
|
@@ -258,36 +230,6 @@ describe Adapter do
|
|
258
230
|
end
|
259
231
|
end
|
260
232
|
|
261
|
-
describe "#[]" do
|
262
|
-
it "is aliased to read" do
|
263
|
-
adapter.write('foo', 'bar')
|
264
|
-
adapter['foo'].should == 'bar'
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "#get" do
|
269
|
-
it "is aliased to read" do
|
270
|
-
adapter.write('foo', 'bar')
|
271
|
-
adapter.get('foo').should == 'bar'
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
describe "#[]=" do
|
276
|
-
it "is aliased to write" do
|
277
|
-
adapter.read('foo').should be_nil
|
278
|
-
adapter['foo'] = 'bar'
|
279
|
-
adapter.read('foo').should == 'bar'
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
describe "#[]=" do
|
284
|
-
it "is aliased to write" do
|
285
|
-
adapter.read('foo').should be_nil
|
286
|
-
adapter.set('foo', 'bar')
|
287
|
-
adapter.read('foo').should == 'bar'
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
233
|
describe "#eql?" do
|
292
234
|
it "returns true if same name and client" do
|
293
235
|
adapter.should eql(Adapter[:memory].new({}))
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module ModuleHelpers
|
2
2
|
def valid_module
|
3
3
|
Module.new do
|
4
|
-
def read(key)
|
5
|
-
|
4
|
+
def read(key, options = nil)
|
5
|
+
client[key]
|
6
6
|
end
|
7
7
|
|
8
|
-
def write(key,
|
9
|
-
client[
|
8
|
+
def write(key, attributes, options = nil)
|
9
|
+
client[key] = attributes
|
10
10
|
end
|
11
11
|
|
12
|
-
def delete(key)
|
13
|
-
client.delete(
|
12
|
+
def delete(key, options = nil)
|
13
|
+
client.delete(key)
|
14
14
|
end
|
15
15
|
|
16
|
-
def clear
|
16
|
+
def clear(options = nil)
|
17
17
|
client.clear
|
18
18
|
end
|
19
19
|
end
|
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.
|
4
|
+
version: 0.7.0
|
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-16 00:00:00.000000000 Z
|
15
15
|
dependencies: []
|
16
16
|
description:
|
17
17
|
email:
|
@@ -33,7 +33,6 @@ files:
|
|
33
33
|
- Rakefile
|
34
34
|
- adapter.gemspec
|
35
35
|
- examples/memory.rb
|
36
|
-
- examples/overriding_serialization.rb
|
37
36
|
- lib/adapter.rb
|
38
37
|
- lib/adapter/asserts.rb
|
39
38
|
- lib/adapter/defaults.rb
|
@@ -41,7 +40,6 @@ files:
|
|
41
40
|
- lib/adapter/memory.rb
|
42
41
|
- lib/adapter/spec/an_adapter.rb
|
43
42
|
- lib/adapter/version.rb
|
44
|
-
- spec/adapter/defaults_spec.rb
|
45
43
|
- spec/adapter/memory_spec.rb
|
46
44
|
- spec/adapter_spec.rb
|
47
45
|
- spec/helper.rb
|
@@ -61,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
59
|
version: '0'
|
62
60
|
segments:
|
63
61
|
- 0
|
64
|
-
hash: -
|
62
|
+
hash: -3971462986522991122
|
65
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
64
|
none: false
|
67
65
|
requirements:
|
@@ -70,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
68
|
version: '0'
|
71
69
|
segments:
|
72
70
|
- 0
|
73
|
-
hash: -
|
71
|
+
hash: -3971462986522991122
|
74
72
|
requirements: []
|
75
73
|
rubyforge_project:
|
76
74
|
rubygems_version: 1.8.23
|
@@ -78,7 +76,6 @@ signing_key:
|
|
78
76
|
specification_version: 3
|
79
77
|
summary: A simple interface to anything
|
80
78
|
test_files:
|
81
|
-
- spec/adapter/defaults_spec.rb
|
82
79
|
- spec/adapter/memory_spec.rb
|
83
80
|
- spec/adapter_spec.rb
|
84
81
|
- spec/helper.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'active_support'
|
3
|
-
require 'pathname'
|
4
|
-
|
5
|
-
root_path = Pathname(__FILE__).dirname.join('..').expand_path
|
6
|
-
lib_path = root_path.join('lib')
|
7
|
-
$:.unshift(lib_path)
|
8
|
-
|
9
|
-
require 'adapter/memory'
|
10
|
-
|
11
|
-
# Adapter.define also takes any combination of module and block.
|
12
|
-
#
|
13
|
-
# If module present, it is included. If block present, it is turned
|
14
|
-
# into a module and included. This means that including a module and
|
15
|
-
# a block allows overriding the module by defining methods in the block.
|
16
|
-
#
|
17
|
-
# In our case below, we simply override the memory adapter to create
|
18
|
-
# a new adapter that encodes/decodes using JSON instead of the default
|
19
|
-
# Marshal.load/dump. Also, important to note that this does not affect
|
20
|
-
# the memory adapter which still uses Marshal.
|
21
|
-
Adapter.define(:memory_json, Adapter::Memory) do
|
22
|
-
def encode(attributes)
|
23
|
-
ActiveSupport::JSON.encode(attributes)
|
24
|
-
end
|
25
|
-
|
26
|
-
def decode(attributes)
|
27
|
-
ActiveSupport::JSON.decode(attributes)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
adapter = Adapter[:memory_json].new({})
|
32
|
-
adapter.clear
|
33
|
-
|
34
|
-
adapter.write('foo', 'bar' => 'baz')
|
35
|
-
# Encoded in adapter as json instead of being marshal'd
|
36
|
-
puts adapter.client['foo'].inspect # "{\"bar\":\"baz\"}"
|
37
|
-
|
38
|
-
|
39
|
-
adapter.client['foo'] = ActiveSupport::JSON.encode('chunky' => 'bacon')
|
40
|
-
# Decoded from adapter using json instead of being un-marshal'd
|
41
|
-
puts adapter.read('foo').inspect # {"chunky"=>"bacon"}
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
describe Adapter::Defaults do
|
4
|
-
let(:mod) do
|
5
|
-
Module.new.tap do |m|
|
6
|
-
m.extend(Adapter::Defaults)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "#key_for" do
|
11
|
-
it "returns whatever is passed to it" do
|
12
|
-
[nil, 'foo', :foo, {:foo => 'bar'}].each do |key|
|
13
|
-
mod.key_for(key).should be(key)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#encode" do
|
19
|
-
it "returns whatever is passed to it" do
|
20
|
-
[nil, 'foo', :foo, {:foo => 'bar'}].each do |value|
|
21
|
-
mod.encode(value).should be(value)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "#decode" do
|
27
|
-
it "returns whatever is passed to it" do
|
28
|
-
[nil, 'foo', :foo, {:foo => 'bar'}].each do |value|
|
29
|
-
mod.decode(value).should be(value)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|