sander6-daijobu 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +65 -0
- data/lib/daijobu.rb +13 -0
- data/lib/daijobu/adapter.rb +17 -0
- data/lib/daijobu/client.rb +28 -0
- data/lib/daijobu/errors.rb +11 -0
- data/lib/daijobu/scheme.rb +20 -0
- data/lib/daijobu/scheme_set.rb +52 -0
- data/spec/daijobu/adapter_spec.rb +59 -0
- data/spec/daijobu/adapters/mem_cache_spec.rb +24 -0
- data/spec/daijobu/adapters/tokyo_cabinet_spec.rb +24 -0
- data/spec/daijobu/adapters/tokyo_tyrant_spec.rb +24 -0
- data/spec/daijobu/client_spec.rb +76 -0
- data/spec/daijobu/errors_spec.rb +33 -0
- data/spec/daijobu/scheme_set_spec.rb +190 -0
- data/spec/daijobu/scheme_spec.rb +39 -0
- data/spec/daijobu/schemes/eval_spec.rb +37 -0
- data/spec/daijobu/schemes/json_spec.rb +37 -0
- data/spec/daijobu/schemes/marshal_spec.rb +37 -0
- data/spec/daijobu/schemes/yaml_spec.rb +37 -0
- data/spec/spec_helper.rb +12 -0
- metadata +74 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Sander Hartlage
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
= Daijobu
|
2
|
+
|
3
|
+
A library for easing serialization and deserialzation of data stuffed into Tokyo Cabinet, including flexible and fallback schemes. This will all make more sense once the documentation is written for real.
|
4
|
+
|
5
|
+
== Copyright
|
6
|
+
|
7
|
+
Copyright (c) 2009 Sander Hartlage. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "daijobu"
|
8
|
+
gem.summary = "A helper for de/serialization of objects into and out of Tokyo Cabinets"
|
9
|
+
gem.email = "sander@outside.in"
|
10
|
+
gem.homepage = "http://github.com/sander6/daijobu"
|
11
|
+
gem.authors = ["Sander Hartlage"]
|
12
|
+
gem.files = %w{
|
13
|
+
LICENSE
|
14
|
+
Rakefile
|
15
|
+
README.rdoc
|
16
|
+
lib/daijobu.rb
|
17
|
+
lib/daijobu/adapter.rb
|
18
|
+
lib/daijobu/client.rb
|
19
|
+
lib/daijobu/errors.rb
|
20
|
+
lib/daijobu/scheme.rb
|
21
|
+
lib/daijobu/scheme_set.rb
|
22
|
+
lib/adapters/mem_cache.rb
|
23
|
+
lib/adapters/tokyo_cabinet.rb
|
24
|
+
lib/adapters/tokyo_tyrant.rb
|
25
|
+
lib/schemes/eval.rb
|
26
|
+
lib/schemes/json.rb
|
27
|
+
lib/schemes/marshal.rb
|
28
|
+
lib/schemes/yaml.rb
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
rescue LoadError
|
33
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'spec/rake/spectask'
|
37
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
38
|
+
spec.libs << 'lib' << 'spec'
|
39
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
40
|
+
end
|
41
|
+
|
42
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
43
|
+
spec.libs << 'lib' << 'spec'
|
44
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
45
|
+
spec.rcov = true
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
task :default => :spec
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
if File.exist?('VERSION.yml')
|
54
|
+
config = YAML.load(File.read('VERSION.yml'))
|
55
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
56
|
+
else
|
57
|
+
version = ""
|
58
|
+
end
|
59
|
+
|
60
|
+
rdoc.rdoc_dir = 'rdoc'
|
61
|
+
rdoc.title = "daijobu #{version}"
|
62
|
+
rdoc.rdoc_files.include('README*')
|
63
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
+
end
|
65
|
+
|
data/lib/daijobu.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
require 'daijobu/errors'
|
3
|
+
require 'daijobu/client'
|
4
|
+
require 'daijobu/scheme_set'
|
5
|
+
require 'daijobu/scheme'
|
6
|
+
require 'daijobu/schemes/marshal'
|
7
|
+
require 'daijobu/schemes/json'
|
8
|
+
require 'daijobu/schemes/yaml'
|
9
|
+
require 'daijobu/schemes/eval'
|
10
|
+
require 'daijobu/adapter'
|
11
|
+
require 'daijobu/adapters/mem_cache'
|
12
|
+
require 'daijobu/adapters/tokyo_cabinet'
|
13
|
+
require 'daijobu/adapters/tokyo_tyrant'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Daijobu
|
2
|
+
module Adapter
|
3
|
+
|
4
|
+
def self.get(casket)
|
5
|
+
if defined?(MemCache) && casket.is_a?(MemCache)
|
6
|
+
Daijobu::Adapter::MemCacheAdapter.new(casket)
|
7
|
+
elsif defined?(Rufus::Tokyo::Cabinet) && casket.is_a?(Rufus::Tokyo::Cabinet)
|
8
|
+
Daijobu::Adapter::TokyoCabinetAdapter.new(casket)
|
9
|
+
elsif defined?(Rufus::Tokyo::Tyrant) && casket.is_a?(Rufus::Tokyo::Tyrant)
|
10
|
+
Daijobu::Adapter::TokyoTyrantAdapter.new(casket)
|
11
|
+
else
|
12
|
+
raise Daijobu::InvalidAdapter
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Daijobu
|
2
|
+
class Client
|
3
|
+
|
4
|
+
def initialize(casket, *schemes)
|
5
|
+
@adapter = Daijobu::Adapter.get(casket)
|
6
|
+
@schemes = Daijobu::SchemeSet.new(*schemes)
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](key)
|
10
|
+
parse(@adapter.get(key))
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
@adapter.set(key, unparse(value))
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def parse(str)
|
20
|
+
@schemes.parse(str)
|
21
|
+
end
|
22
|
+
|
23
|
+
def unparse(obj)
|
24
|
+
@schemes.unparse(obj)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Daijobu
|
2
|
+
module Scheme
|
3
|
+
|
4
|
+
def self.get(name)
|
5
|
+
case name
|
6
|
+
when :marshal
|
7
|
+
Daijobu::Scheme::Marshal.new
|
8
|
+
when :json
|
9
|
+
Daijobu::Scheme::JSON.new
|
10
|
+
when :yaml
|
11
|
+
Daijobu::Scheme::YAML.new
|
12
|
+
when :eval
|
13
|
+
Daijobu::Scheme::Eval.new
|
14
|
+
else
|
15
|
+
raise Daijobu::UnknownScheme
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Daijobu
|
2
|
+
class SchemeSet
|
3
|
+
|
4
|
+
DEFAULT = [ :marshal, :json, :yaml, :eval ]
|
5
|
+
|
6
|
+
attr_reader :current
|
7
|
+
|
8
|
+
def initialize(*schemes)
|
9
|
+
@schemes = (schemes.empty? ? DEFAULT : schemes).collect { |scheme| Daijobu::Scheme.get(scheme) }
|
10
|
+
@current = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def next
|
14
|
+
scheme = @schemes[@current]
|
15
|
+
raise NoFallbackScheme unless scheme
|
16
|
+
@current += 1
|
17
|
+
return scheme
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset
|
21
|
+
@current = 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse(str)
|
25
|
+
begin
|
26
|
+
obj = self.next.parse(str)
|
27
|
+
rescue => e
|
28
|
+
if e.kind_of?(Daijobu::Error)
|
29
|
+
raise e
|
30
|
+
else
|
31
|
+
obj = parse(str)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
self.reset
|
35
|
+
obj
|
36
|
+
end
|
37
|
+
|
38
|
+
def unparse(obj)
|
39
|
+
begin
|
40
|
+
str = self.next.unparse(obj)
|
41
|
+
rescue => e
|
42
|
+
if e.kind_of?(Daijobu::Error)
|
43
|
+
raise e
|
44
|
+
else
|
45
|
+
str = unparse(str)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
self.reset
|
49
|
+
str
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Adapter do
|
4
|
+
|
5
|
+
describe ".get" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
Daijobu::Adapter.stubs(:defined?).returns(true)
|
9
|
+
MemCache = Class.new unless defined?(MemCache)
|
10
|
+
Rufus::Tokyo::Cabinet = Class.new unless defined?(Rufus::Tokyo::Cabinet)
|
11
|
+
Rufus::Tokyo::Tyrant = Class.new unless defined?(Rufus::Tokyo::Tyrant)
|
12
|
+
@casket = stub("fake casket")
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "with a MemCache instance" do
|
16
|
+
before do
|
17
|
+
@casket.stubs(:is_a?).with(MemCache).returns(true)
|
18
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Cabinet).returns(false)
|
19
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Tyrant).returns(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return a MemCache adapter" do
|
23
|
+
Daijobu::Adapter.get(@casket).should be_an_instance_of(Daijobu::Adapter::MemCacheAdapter)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "with a Rufus::Tokyo::Cabinet instance" do
|
28
|
+
before do
|
29
|
+
@casket.stubs(:is_a?).with(MemCache).returns(false)
|
30
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Cabinet).returns(true)
|
31
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Tyrant).returns(false)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return a Tokyo Cabinet adapter" do
|
35
|
+
Daijobu::Adapter.get(@casket).should be_an_instance_of(Daijobu::Adapter::TokyoCabinetAdapter)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with a Rufus::Tokyo::Tyrant instance" do
|
40
|
+
before do
|
41
|
+
@casket.stubs(:is_a?).with(MemCache).returns(false)
|
42
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Cabinet).returns(false)
|
43
|
+
@casket.stubs(:is_a?).with(Rufus::Tokyo::Tyrant).returns(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return a Tokyo Tyrant adapter" do
|
47
|
+
Daijobu::Adapter.get(@casket).should be_an_instance_of(Daijobu::Adapter::TokyoTyrantAdapter)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "with anything else" do
|
52
|
+
it "should raise an error" do
|
53
|
+
lambda { Daijobu::Adapter.get(:bogus) }.should raise_error(Daijobu::InvalidAdapter)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Adapter::MemCacheAdapter do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@stubby = stub('fake store')
|
7
|
+
@adapter = Daijobu::Adapter::MemCacheAdapter.new(@stubby)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#get" do
|
11
|
+
it "should call #get on the store and read as raw" do
|
12
|
+
@stubby.expects(:get).with('key', true)
|
13
|
+
@adapter.get('key')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "set" do
|
18
|
+
it "should call #set on the store and write as raw" do
|
19
|
+
@stubby.expects(:set).with('key', 'value', 0, true)
|
20
|
+
@adapter.set('key', 'value')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Adapter::TokyoCabinetAdapter do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@stubby = stub('fake store')
|
7
|
+
@adapter = Daijobu::Adapter::TokyoCabinetAdapter.new(@stubby)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#get" do
|
11
|
+
it "should call #[] on the store" do
|
12
|
+
@stubby.expects(:[]).with('key')
|
13
|
+
@adapter.get('key')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "set" do
|
18
|
+
it "should call #set on the store" do
|
19
|
+
@stubby.expects(:[]=).with('key', 'value')
|
20
|
+
@adapter.set('key', 'value')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Adapter::TokyoTyrantAdapter do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@stubby = stub('fake store')
|
7
|
+
@adapter = Daijobu::Adapter::TokyoTyrantAdapter.new(@stubby)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#get" do
|
11
|
+
it "should call #[] on the store" do
|
12
|
+
@stubby.expects(:[]).with('key')
|
13
|
+
@adapter.get('key')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "set" do
|
18
|
+
it "should call #set on the store and write as raw" do
|
19
|
+
@stubby.expects(:[]=).with('key', 'value')
|
20
|
+
@adapter.set('key', 'value')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@casket = Rufus::Tokyo::Cabinet.new('*')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "initialization" do
|
10
|
+
|
11
|
+
it "should set the adapter based on the casket object given" do
|
12
|
+
Daijobu::Adapter.expects(:get).with(@casket)
|
13
|
+
Daijobu::Client.new(@casket)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should set the schemes based on the scheme list given" do
|
17
|
+
Daijobu::SchemeSet.expects(:new).with(:json, :yaml)
|
18
|
+
Daijobu::Client.new(@casket, :json, :yaml)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "methods" do
|
24
|
+
before do
|
25
|
+
@daijobu = Daijobu::Client.new(@casket)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "for getting and setting" do
|
29
|
+
before do
|
30
|
+
@adapter = @daijobu.instance_variable_get(:@adapter)
|
31
|
+
@stringy = "woohoo!"
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#[]" do
|
35
|
+
it "should call #get on the adapter" do
|
36
|
+
@adapter.expects(:get).with('key')
|
37
|
+
@daijobu['key']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#[]=" do
|
42
|
+
before do
|
43
|
+
@daijobu.stubs(:unparse).returns(@stringy)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should call set on the adapter" do
|
47
|
+
@adapter.expects(:set).with('key', @stringy)
|
48
|
+
@daijobu['key'] = @stringy
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
describe "for parsing and unparsing" do
|
55
|
+
before do
|
56
|
+
@schemes = @daijobu.instance_variable_get(:@schemes)
|
57
|
+
@stringy = "woohoo!"
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#parse" do
|
61
|
+
it "should call parse on the schemes" do
|
62
|
+
@schemes.expects(:parse).with(@stringy)
|
63
|
+
@daijobu.__send__(:parse, @stringy)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#unparse" do
|
68
|
+
it "should call unparse on the schemes" do
|
69
|
+
@schemes.expects(:unparse).with(@stringy)
|
70
|
+
@daijobu.__send__(:unparse, @stringy)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Error do
|
4
|
+
|
5
|
+
it "should be a standard error" do
|
6
|
+
Daijobu::Error.ancestors.should include(StandardError)
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Daijobu::UnknownScheme do
|
12
|
+
|
13
|
+
it "should be a Daijobu error" do
|
14
|
+
Daijobu::UnknownScheme.ancestors.should include(Daijobu::Error)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Daijobu::InvalidAdapter do
|
20
|
+
|
21
|
+
it "should be a Daijobu error" do
|
22
|
+
Daijobu::InvalidAdapter.ancestors.should include(Daijobu::Error)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Daijobu::NoFallbackScheme do
|
28
|
+
|
29
|
+
it "should be a Daijobu error" do
|
30
|
+
Daijobu::NoFallbackScheme.ancestors.should include(Daijobu::Error)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::SchemeSet do
|
4
|
+
|
5
|
+
describe "initialization" do
|
6
|
+
before do
|
7
|
+
@scheme_set = Daijobu::SchemeSet.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should set current to 0" do
|
11
|
+
@scheme_set.current.should == 0
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should have the default set of schemes" do
|
15
|
+
schemes = @scheme_set.instance_variable_get(:@schemes)
|
16
|
+
schemes[0].should be_an_instance_of(Daijobu::Scheme::Marshal)
|
17
|
+
schemes[1].should be_an_instance_of(Daijobu::Scheme::JSON)
|
18
|
+
schemes[2].should be_an_instance_of(Daijobu::Scheme::YAML)
|
19
|
+
schemes[3].should be_an_instance_of(Daijobu::Scheme::Eval)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "initialization with a specified scheme pattern" do
|
25
|
+
before do
|
26
|
+
@scheme_set = Daijobu::SchemeSet.new(:eval, :yaml)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have the proper schemes in the given order" do
|
30
|
+
schemes = @scheme_set.instance_variable_get(:@schemes)
|
31
|
+
schemes[0].should be_an_instance_of(Daijobu::Scheme::Eval)
|
32
|
+
schemes[1].should be_an_instance_of(Daijobu::Scheme::YAML)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#next" do
|
37
|
+
before do
|
38
|
+
@scheme_set = Daijobu::SchemeSet.new
|
39
|
+
@schemes = @scheme_set.instance_variable_get(:@schemes)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return the first of the schemes if #next hasn't been called since the last reset" do
|
43
|
+
@scheme_set.next.should == @schemes[0]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should advance @current by 1" do
|
47
|
+
old = @scheme_set.current
|
48
|
+
@scheme_set.next
|
49
|
+
@scheme_set.current.should == old + 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return the next of the schemes on each subsequent calling" do
|
53
|
+
@scheme_set.next.should == @schemes[0]
|
54
|
+
@scheme_set.next.should == @schemes[1]
|
55
|
+
@scheme_set.next.should == @schemes[2]
|
56
|
+
@scheme_set.next.should == @schemes[3]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise an error when there are no schemes left to try" do
|
60
|
+
lambda { (@schemes.size + 1).times { @scheme_set.next } }.should raise_error(Daijobu::NoFallbackScheme)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#reset" do
|
65
|
+
before do
|
66
|
+
@scheme_set = Daijobu::SchemeSet.new
|
67
|
+
@schemes = @scheme_set.instance_variable_get(:@schemes)
|
68
|
+
@first_scheme = @scheme_set.next
|
69
|
+
@scheme_set.current.should_not == 0
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should reset current to 0" do
|
73
|
+
@scheme_set.reset
|
74
|
+
@scheme_set.current.should == 0
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should then make the next subsequent calling of #next return the first scheme" do
|
78
|
+
@scheme_set.reset
|
79
|
+
@scheme_set.next.should == @first_scheme
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#parse" do
|
84
|
+
before do
|
85
|
+
@scheme_set = Daijobu::SchemeSet.new(:json)
|
86
|
+
@stringy = '{ "thing" : 10 }'
|
87
|
+
@hashy = { "thing" => 10 }
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "assuming that the string can be parsed" do
|
91
|
+
before do
|
92
|
+
Daijobu::Scheme::JSON.any_instance.stubs(:parse).returns(@hashy)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should reset" do
|
96
|
+
@scheme_set.expects(:reset)
|
97
|
+
@scheme_set.parse(@stringy)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return the parsed entity" do
|
101
|
+
@scheme_set.parse(@stringy).should == @hashy
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "assuming that the string can't be parsed" do
|
106
|
+
before do
|
107
|
+
Daijobu::Scheme::JSON.any_instance.stubs(:parse).raises(Daijobu::Error)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should raise the given error" do
|
111
|
+
lambda { @scheme_set.parse(@stringy) }.should raise_error(Daijobu::Error)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "#parse, when the first (or any earlier) scheme doesn't work" do
|
117
|
+
before do
|
118
|
+
@stringy = '{ "thing" : 10 }'
|
119
|
+
@hashy = { "thing" => 10 }
|
120
|
+
|
121
|
+
@scheme_set = Daijobu::SchemeSet.new(:marshal, :yaml, :json)
|
122
|
+
@schemes = @scheme_set.instance_variable_get(:@schemes)
|
123
|
+
@marshal_scheme = @schemes[0]
|
124
|
+
@yaml_scheme = @schemes[1]
|
125
|
+
@json_scheme = @schemes[2]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should keep trying to parse with subsequent schemes" do
|
129
|
+
@marshal_scheme.expects(:parse).raises(TypeError)
|
130
|
+
@yaml_scheme.expects(:parse).raises(ArgumentError)
|
131
|
+
@json_scheme.expects(:parse).returns(@stringy)
|
132
|
+
|
133
|
+
@scheme_set.parse(@stringy)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#unparse" do
|
138
|
+
before do
|
139
|
+
@scheme_set = Daijobu::SchemeSet.new(:json)
|
140
|
+
@stringy = '{ "thing" : 10 }'
|
141
|
+
@hashy = { "thing" => 10 }
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "assuming that the object can be unparsed" do
|
145
|
+
before do
|
146
|
+
Daijobu::Scheme::JSON.any_instance.stubs(:unparse).returns(@stringy)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should return the unparsed entity" do
|
150
|
+
@scheme_set.unparse(@hashy).should == @stringy
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should reset" do
|
154
|
+
@scheme_set.expects(:reset)
|
155
|
+
@scheme_set.unparse(@hashy)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "assuming that the object can't be unparsed" do
|
160
|
+
before do
|
161
|
+
Daijobu::Scheme::JSON.any_instance.stubs(:unparse).raises(Daijobu::Error)
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should raise the given error" do
|
165
|
+
lambda { @scheme_set.unparse(@hashy) }.should raise_error(Daijobu::Error)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#unparse, when the first (or any earlier) scheme doesn't work" do
|
171
|
+
before do
|
172
|
+
@stringy = '{ "thing" : 10 }'
|
173
|
+
@hashy = { "thing" => 10 }
|
174
|
+
|
175
|
+
@scheme_set = Daijobu::SchemeSet.new(:marshal, :yaml, :json)
|
176
|
+
@schemes = @scheme_set.instance_variable_get(:@schemes)
|
177
|
+
@marshal_scheme = @schemes[0]
|
178
|
+
@yaml_scheme = @schemes[1]
|
179
|
+
@json_scheme = @schemes[2]
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should keep trying to unparse with subsequent schemes" do
|
183
|
+
@marshal_scheme.expects(:unparse).raises(TypeError)
|
184
|
+
@yaml_scheme.expects(:unparse).raises(ArgumentError)
|
185
|
+
@json_scheme.expects(:unparse).returns(@stringy)
|
186
|
+
|
187
|
+
@scheme_set.unparse(@hashy)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Scheme do
|
4
|
+
|
5
|
+
describe ".get" do
|
6
|
+
|
7
|
+
describe "with :marshal" do
|
8
|
+
it "should return a marshal scheme" do
|
9
|
+
Daijobu::Scheme.get(:marshal).should be_an_instance_of(Daijobu::Scheme::Marshal)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with :json" do
|
14
|
+
it "should return a json scheme" do
|
15
|
+
Daijobu::Scheme.get(:json).should be_an_instance_of(Daijobu::Scheme::JSON)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "with :yaml" do
|
20
|
+
it "should return a yaml scheme" do
|
21
|
+
Daijobu::Scheme.get(:yaml).should be_an_instance_of(Daijobu::Scheme::YAML)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "with :eval" do
|
26
|
+
it "should return a eval scheme" do
|
27
|
+
Daijobu::Scheme.get(:eval).should be_an_instance_of(Daijobu::Scheme::Eval)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "with anything else" do
|
32
|
+
it "should raise an error" do
|
33
|
+
lambda { Daijobu::Scheme.get(:xml) }.should raise_error(Daijobu::UnknownScheme)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Scheme::Eval do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@scheme = Daijobu::Scheme::Eval.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#parse" do
|
10
|
+
before do
|
11
|
+
@stringy = '{ "thing" => 10 }'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should parse the given string using Kernel::eval" do
|
15
|
+
@scheme.expects(:eval).with(@stringy)
|
16
|
+
@scheme.parse(@stringy)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "when the input string is nil" do
|
20
|
+
it "should return nil" do
|
21
|
+
@scheme.parse(nil).should be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#unparse" do
|
27
|
+
before do
|
28
|
+
@hashy = { "thing" => 10 }
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should inspect the given object" do
|
32
|
+
@hashy.expects(:inspect)
|
33
|
+
@scheme.unparse(@hashy)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Scheme::JSON do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@scheme = Daijobu::Scheme::JSON.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#parse" do
|
10
|
+
before do
|
11
|
+
@stringy = '{ "thing" : 10 }'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should parse the given string with the JSON module" do
|
15
|
+
::JSON.expects(:parse).with(@stringy)
|
16
|
+
@scheme.parse(@stringy)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "when the input string is nil" do
|
20
|
+
it "should return nil" do
|
21
|
+
@scheme.parse(nil).should be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#unparse" do
|
27
|
+
before do
|
28
|
+
@hashy = { "thing" => 10 }
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should unparse the given object with the JSON module" do
|
32
|
+
::JSON.expects(:unparse).with(@hashy)
|
33
|
+
@scheme.unparse(@hashy)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Scheme::Marshal do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@scheme = Daijobu::Scheme::Marshal.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#parse" do
|
10
|
+
before do
|
11
|
+
@stringy = "\004\b{\006\"\nthingi\017"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should parse the given string with the Marshal module" do
|
15
|
+
::Marshal.expects(:load).with(@stringy)
|
16
|
+
@scheme.parse(@stringy)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "when the input string is nil" do
|
20
|
+
it "should return nil" do
|
21
|
+
@scheme.parse(nil).should be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#unparse" do
|
27
|
+
before do
|
28
|
+
@hashy = { "thing" => 10 }
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should unparse the given object with the JSON module" do
|
32
|
+
::Marshal.expects(:dump).with(@hashy)
|
33
|
+
@scheme.unparse(@hashy)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Daijobu::Scheme::YAML do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@scheme = Daijobu::Scheme::YAML.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#parse" do
|
10
|
+
before do
|
11
|
+
@stringy = '{ "thing" : 10 }'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should parse the given string with the YAML module" do
|
15
|
+
::YAML.expects(:load).with(@stringy)
|
16
|
+
@scheme.parse(@stringy)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "when the input string is nil" do
|
20
|
+
it "should return nil" do
|
21
|
+
@scheme.parse(nil).should be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#unparse" do
|
27
|
+
before do
|
28
|
+
@hashy = { "thing" => 10 }
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should unparse the given object with the JSON module" do
|
32
|
+
::YAML.expects(:dump).with(@hashy)
|
33
|
+
@scheme.unparse(@hashy)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'mocha'
|
4
|
+
require 'rufus/tokyo'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
require 'daijobu'
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
config.mock_with :mocha
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sander6-daijobu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sander Hartlage
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-14 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: sander@outside.in
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- lib/daijobu.rb
|
30
|
+
- lib/daijobu/adapter.rb
|
31
|
+
- lib/daijobu/client.rb
|
32
|
+
- lib/daijobu/errors.rb
|
33
|
+
- lib/daijobu/scheme.rb
|
34
|
+
- lib/daijobu/scheme_set.rb
|
35
|
+
has_rdoc: false
|
36
|
+
homepage: http://github.com/sander6/daijobu
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: A helper for de/serialization of objects into and out of Tokyo Cabinets
|
61
|
+
test_files:
|
62
|
+
- spec/daijobu/adapter_spec.rb
|
63
|
+
- spec/daijobu/adapters/mem_cache_spec.rb
|
64
|
+
- spec/daijobu/adapters/tokyo_cabinet_spec.rb
|
65
|
+
- spec/daijobu/adapters/tokyo_tyrant_spec.rb
|
66
|
+
- spec/daijobu/client_spec.rb
|
67
|
+
- spec/daijobu/errors_spec.rb
|
68
|
+
- spec/daijobu/scheme_set_spec.rb
|
69
|
+
- spec/daijobu/scheme_spec.rb
|
70
|
+
- spec/daijobu/schemes/eval_spec.rb
|
71
|
+
- spec/daijobu/schemes/json_spec.rb
|
72
|
+
- spec/daijobu/schemes/marshal_spec.rb
|
73
|
+
- spec/daijobu/schemes/yaml_spec.rb
|
74
|
+
- spec/spec_helper.rb
|