Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move to a more specific model of what toystore needs.

Rather than have a generic adapter library, I think it is better to have something that more specifically adapts toystore to different data stores.
  • Loading branch information...
commit f298ed20dd4d2ee0b8e3fc2ebffaa0e134cbef0e 1 parent fb4ac00
@jnunemaker authored
View
1  Gemfile
@@ -7,7 +7,6 @@ group(:guard) do
gem 'guard'
gem 'guard-bundler'
gem 'guard-rspec'
- gem 'growl'
end
group(:test) do
View
2  Gemfile.lock
@@ -8,7 +8,6 @@ GEM
specs:
diff-lcs (1.1.3)
ffi (1.0.11)
- growl (1.0.3)
guard (1.0.0)
ffi (>= 0.5.0)
thor (~> 0.14.6)
@@ -33,7 +32,6 @@ PLATFORMS
DEPENDENCIES
adapter!
- growl
guard
guard-bundler
guard-rspec
View
1  Guardfile
@@ -7,6 +7,7 @@ guard 'rspec', rspec_options do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/helper.rb') { "spec" }
+ watch(%r{lib/adapter/spec/(.+)\.rb}) { "spec" }
end
guard 'bundler' do
View
12 lib/adapter/defaults.rb
@@ -12,21 +12,15 @@ def key?(key)
end
def key_for(key)
- if key.is_a?(String)
- key
- elsif key.is_a?(Symbol)
- key.to_s
- else
- Marshal.dump(key)
- end
+ key
end
def encode(value)
- Marshal.dump(value)
+ value
end
def decode(value)
- value && Marshal.load(value)
+ value
end
end
end
View
171 lib/adapter/spec/an_adapter.rb
@@ -3,118 +3,117 @@
adapter.client.should == client
end
- Adapter::Spec::Types.each do |type, (key, key2)|
- it "reads from keys that are #{type}s like a Hash" do
- adapter[key].should == nil
+ let(:key) { 'key' }
+ let(:key2) { 'key2' }
+
+ let(:attributes) {
+ {
+ :one => 'one',
+ }
+ }
+
+ let(:attributes2) {
+ {
+ :two => 'two',
+ }
+ }
+
+ describe "#read" do
+ it "returns nil if key not available" do
+ adapter.read(key).should be_nil
end
- it "writes String values to keys that are #{type}s like a Hash" do
- adapter[key] = "value"
- adapter[key].should == "value"
- end
-
- it "guarantees that a different String value is retrieved from the #{type} key" do
- value = "value"
- adapter[key] = value
- adapter[key].should_not be_equal(value)
+ it "returns value if key available" do
+ adapter.write(key, attributes)
+ adapter.read(key).should eq(attributes)
end
+ end
- it "guarantees that a different Object value is retrieved from the #{type} key" do
- value = {:foo => :bar}
- adapter[key] = value
- adapter[key].should_not be_equal(:foo => :bar)
+ describe "#[]" do
+ it "returns nil if key not available" do
+ adapter[key].should be_nil
end
- it "returns false from key? if a #{type} key is not available" do
- adapter.key?(key).should be_false
+ it "returns value if key available" do
+ adapter.write(key, attributes)
+ adapter[key].should eq(attributes)
end
+ end
- it "returns true from key? if a #{type} key is available" do
- adapter[key] = "value"
+ describe "#key?" do
+ it "returns true if key available" do
+ adapter.write(key, attributes)
adapter.key?(key).should be_true
end
- it "removes and return an element with a #{type} key from the backing store via delete if it exists" do
- adapter[key] = "value"
- adapter.delete(key).should == "value"
+ it "returns false if key not available" do
adapter.key?(key).should be_false
end
+ end
- it "returns nil from delete if an element for a #{type} key does not exist" do
- adapter.delete(key).should be_nil
+ describe "#fetch" do
+ context "with key not stored" do
+ context "with default value" do
+ it "returns default value" do
+ adapter.fetch(key, {}).should eq({})
+ end
+ end
+
+ context "with default block" do
+ it "returns value of yielded block" do
+ adapter.fetch(key) { |k| {} }.should eq({})
+ end
+ end
end
- it "removes all #{type} keys from the store with clear" do
- adapter[key] = "value"
- adapter[key2] = "value2"
- adapter.clear
- adapter.key?(key).should_not be_true
- adapter.key?(key2).should_not be_true
+ context "with key that is stored" do
+ context "with default value" do
+ it "returns key value instead of default" do
+ adapter.write(key, attributes2)
+ adapter.fetch(key, attributes).should eq(attributes2)
+ end
+ end
+
+ context "with default block" do
+ it "does not run the block" do
+ adapter.write(key, attributes)
+ unaltered = 'unaltered'
+ adapter.fetch(key) { unaltered = 'altered' }
+ unaltered.should eq('unaltered')
+ end
+ end
end
+ end
- it "fetches a #{type} key with a default value with fetch, if the key is not available" do
- adapter.fetch(key, "value").should == "value"
+ describe "#write" do
+ it "sets key to value" do
+ adapter.write(key, attributes)
+ adapter.read(key).should eq(attributes)
end
+ end
- it "fetches a #{type} key with a block with fetch, if the key is not available" do
- adapter.fetch(key) { |k| "value" }.should == "value"
+ describe "#delete" do
+ it "removes stored key from store and returns value" do
+ adapter.write(key, attributes)
+ adapter.delete(key).should eq(attributes)
+ adapter.key?(key).should be_false
end
- it "does not run the block if the #{type} key is available" do
- adapter[key] = "value"
- unaltered = "unaltered"
- adapter.fetch(key) { unaltered = "altered" }
- unaltered.should == "unaltered"
+ it "returns nil when key not stored" do
+ adapter.delete(key).should be_nil
end
+ end
- it "fetches a #{type} key with a default value with fetch, if the key is available" do
- adapter[key] = "value2"
- adapter.fetch(key, "value").should == "value2"
+ describe "#clear" do
+ before do
+ adapter[key] = attributes
+ adapter[key2] = attributes2
+ adapter.clear
end
- it "writes #{key} values with #write" do
- adapter.write(key, "value")
- adapter[key].should == "value"
+ it "removes all stored keys" do
+ adapter.key?(key).should be_false
+ adapter.key?(key2).should be_false
end
end
-
- it "refuses to #[] from keys that cannot be marshalled" do
- lambda do
- adapter[Struct.new(:foo).new(:bar)]
- end.should raise_error(TypeError)
- end
-
- it "refuses to fetch from keys that cannot be marshalled" do
- lambda do
- adapter.fetch(Struct.new(:foo).new(:bar), true)
- end.should raise_error(TypeError)
- end
-
- it "refuses to #[]= to keys that cannot be marshalled" do
- lambda do
- adapter[Struct.new(:foo).new(:bar)] = "value"
- end.should raise_error(TypeError)
- end
-
- it "refuses to store to keys that cannot be marshalled" do
- lambda do
- adapter.write Struct.new(:foo).new(:bar), "value"
- end.should raise_error(TypeError)
- end
-
- it "refuses to check for key? if the key cannot be marshalled" do
- lambda do
- adapter.key? Struct.new(:foo).new(:bar)
- end.should raise_error(TypeError)
- end
-
- it "refuses to delete a key if the key cannot be marshalled" do
- lambda do
- adapter.delete Struct.new(:foo).new(:bar)
- end.should raise_error(TypeError)
- end
-
- it "specifies that it is writable via frozen?" do
- adapter.should_not be_frozen
- end
end
View
10 lib/adapter/spec/json_adapter.rb
@@ -1,10 +0,0 @@
-shared_examples_for "a json adapter" do
- it_should_behave_like 'an adapter'
-
- Adapter::Spec::Types.each do |type, (key, key2)|
- it "writes Object values to keys that are #{type}s like a Hash" do
- adapter[key] = {:foo => :bar}
- adapter[key].should == {'foo' => 'bar'}
- end
- end
-end
View
10 lib/adapter/spec/marshal_adapter.rb
@@ -1,10 +0,0 @@
-shared_examples_for "a marshaled adapter" do
- it_should_behave_like 'an adapter'
-
- Adapter::Spec::Types.each do |type, (key, key2)|
- it "writes Object values to keys that are #{type}s like a Hash" do
- adapter[key] = {:foo => :bar}
- adapter[key].should == {:foo => :bar}
- end
- end
-end
View
5 lib/adapter/spec/types.rb
@@ -1,5 +0,0 @@
-module Adapter
- module Spec
- Types = {"String" => ["key", "key2"]}
- end
-end
View
31 spec/adapter/defaults_spec.rb
@@ -8,33 +8,26 @@
end
describe "#key_for" do
- it "returns value for string" do
- mod.key_for('foo').should == 'foo'
- end
-
- it "returns string for symbol" do
- mod.key_for(:foo).should == 'foo'
- end
-
- it "marshals anything not a string or symbol" do
- mod.key_for({'testing' => 'this'}).should == Marshal.dump({'testing' => 'this'})
+ it "returns whatever is passed to it" do
+ [nil, 'foo', :foo, {:foo => 'bar'}].each do |key|
+ mod.key_for(key).should be(key)
+ end
end
end
describe "#encode" do
- it "marshals value" do
- mod.encode(nil).should == Marshal.dump(nil)
- mod.encode({'testing' => 'this'}).should == Marshal.dump({'testing' => 'this'})
+ it "returns whatever is passed to it" do
+ [nil, 'foo', :foo, {:foo => 'bar'}].each do |value|
+ mod.encode(value).should be(value)
+ end
end
end
describe "#decode" do
- it "returns nil if nil" do
- mod.decode(nil).should be_nil
- end
-
- it "returns marshal load if not nil" do
- mod.decode(%Q(\004\b{\006\"\ftesting\"\tthis)).should == {'testing' => 'this'}
+ it "returns whatever is passed to it" do
+ [nil, 'foo', :foo, {:foo => 'bar'}].each do |value|
+ mod.decode(value).should be(value)
+ end
end
end
end
View
2  spec/adapter/memory_spec.rb
@@ -12,5 +12,5 @@
let(:adapter) { @adapter }
let(:client) { @client }
- it_should_behave_like 'a marshaled adapter'
+ it_should_behave_like 'an adapter'
end
View
8 spec/adapter_spec.rb
@@ -114,11 +114,11 @@ def clear
before do
Adapter.define(:memory_json, valid_module) do
def encode(value)
- value
+ 'encoded'
end
def decode(value)
- value
+ 'decoded'
end
end
end
@@ -127,13 +127,13 @@ def decode(value)
it "encodes correctly" do
hash = {'foo' => 'bar'}
adapter.write('foo', hash)
- adapter.client['foo'].should == hash
+ adapter.client['foo'].should == 'encoded'
end
it "decodes correctly" do
hash = {'foo' => 'bar'}
adapter.client['foo'] = hash
- adapter.read('foo').should == hash
+ adapter.read('foo').should == 'decoded'
end
end
View
2  spec/helper.rb
@@ -5,8 +5,6 @@
require 'bundler'
require 'adapter/spec/an_adapter'
-require 'adapter/spec/marshal_adapter'
-require 'adapter/spec/types'
require 'support/module_helpers'
Bundler.require(:default, :test)
Please sign in to comment.
Something went wrong with that request. Please try again.