gom 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -0
- data/Rakefile +1 -1
- data/lib/gom.rb +1 -0
- data/lib/gom/object.rb +15 -19
- data/lib/gom/object/builder.rb +35 -33
- data/lib/gom/object/cached_builder.rb +23 -31
- data/lib/gom/object/collection.rb +49 -57
- data/lib/gom/object/draft.rb +17 -25
- data/lib/gom/object/id.rb +14 -22
- data/lib/gom/object/inspector.rb +46 -54
- data/lib/gom/object/mapping.rb +31 -47
- data/lib/gom/object/proxy.rb +25 -33
- data/lib/gom/spec.rb +11 -0
- data/lib/gom/spec/acceptance/adapter_that_needs_setup.rb +58 -0
- data/lib/gom/spec/acceptance/adapter_with_stateful_storage.rb +19 -21
- data/lib/gom/spec/acceptance/read_only_adapter_with_stateless_storage.rb +9 -8
- data/lib/gom/spec/object.rb +8 -0
- data/lib/gom/storage.rb +30 -26
- data/lib/gom/storage/adapter.rb +26 -43
- data/lib/gom/storage/configuration.rb +75 -68
- data/lib/gom/storage/configuration/view.rb +3 -16
- data/lib/gom/storage/configuration/view/class.rb +5 -22
- data/lib/gom/storage/configuration/view/map_reduce.rb +7 -24
- data/lib/gom/storage/fetcher.rb +24 -32
- data/lib/gom/storage/remover.rb +27 -35
- data/lib/gom/storage/saver.rb +35 -43
- data/spec/acceptance/adapter_spec.rb +16 -4
- data/spec/acceptance/fake_adapter_spec.rb +11 -0
- data/spec/acceptance/object_spec.rb +4 -1
- data/spec/fake_adapter.rb +12 -3
- data/spec/lib/gom/object/builder_spec.rb +5 -5
- data/spec/lib/gom/object/inspector_spec.rb +5 -9
- data/spec/lib/gom/object/mapping_spec.rb +27 -57
- data/spec/lib/gom/storage/adapter_spec.rb +1 -1
- data/spec/lib/gom/storage/configuration_spec.rb +33 -3
- data/spec/lib/gom/storage_spec.rb +19 -0
- metadata +50 -46
data/lib/gom/storage/fetcher.rb
CHANGED
@@ -1,44 +1,36 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
# Fetches an object from the storage.
|
3
|
+
class GOM::Storage::Fetcher
|
3
4
|
|
4
|
-
|
5
|
+
attr_accessor :id
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attr_accessor :id
|
10
|
-
|
11
|
-
def initialize(id)
|
12
|
-
@id = id
|
13
|
-
end
|
14
|
-
|
15
|
-
def object
|
16
|
-
fetch_draft
|
17
|
-
return unless has_draft?
|
18
|
-
build_object
|
19
|
-
@object
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
7
|
+
def initialize(id)
|
8
|
+
@id = id
|
9
|
+
end
|
23
10
|
|
24
|
-
|
25
|
-
|
26
|
-
|
11
|
+
def object
|
12
|
+
fetch_draft
|
13
|
+
return unless has_draft?
|
14
|
+
build_object
|
15
|
+
@object
|
16
|
+
end
|
27
17
|
|
28
|
-
|
29
|
-
!!@draft
|
30
|
-
end
|
18
|
+
private
|
31
19
|
|
32
|
-
|
33
|
-
|
34
|
-
|
20
|
+
def fetch_draft
|
21
|
+
@draft = @id ? adapter.fetch(@id.object_id) : nil
|
22
|
+
end
|
35
23
|
|
36
|
-
|
37
|
-
|
38
|
-
|
24
|
+
def has_draft?
|
25
|
+
!!@draft
|
26
|
+
end
|
39
27
|
|
40
|
-
|
28
|
+
def build_object
|
29
|
+
@object = GOM::Object::CachedBuilder.new(@draft, @id).object
|
30
|
+
end
|
41
31
|
|
32
|
+
def adapter
|
33
|
+
@adapter ||= GOM::Storage::Configuration[@id.storage_name].adapter
|
42
34
|
end
|
43
35
|
|
44
36
|
end
|
data/lib/gom/storage/remover.rb
CHANGED
@@ -1,47 +1,39 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
# Removes an object from the storage.
|
3
|
+
class GOM::Storage::Remover
|
3
4
|
|
4
|
-
|
5
|
+
attr_reader :object
|
6
|
+
attr_reader :id
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def initialize(object_or_id)
|
13
|
-
@object, @id = object_or_id.is_a?(GOM::Object::Id) ?
|
14
|
-
[ nil, object_or_id ] :
|
15
|
-
[ object_or_id, nil ]
|
16
|
-
end
|
17
|
-
|
18
|
-
def perform
|
19
|
-
check_mapping
|
20
|
-
remove_object
|
21
|
-
remove_mapping
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
8
|
+
def initialize(object_or_id)
|
9
|
+
@object, @id = object_or_id.is_a?(GOM::Object::Id) ?
|
10
|
+
[ nil, object_or_id ] :
|
11
|
+
[ object_or_id, nil ]
|
12
|
+
end
|
25
13
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
14
|
+
def perform
|
15
|
+
check_mapping
|
16
|
+
remove_object
|
17
|
+
remove_mapping
|
18
|
+
end
|
30
19
|
|
31
|
-
|
32
|
-
adapter.remove @id.object_id
|
33
|
-
end
|
20
|
+
private
|
34
21
|
|
35
|
-
|
36
|
-
|
37
|
-
|
22
|
+
def check_mapping
|
23
|
+
@id ||= GOM::Object::Mapping.id_by_object @object
|
24
|
+
raise ArgumentError, "No id existing for the given object!" unless @id
|
25
|
+
end
|
38
26
|
|
39
|
-
|
40
|
-
|
41
|
-
|
27
|
+
def remove_object
|
28
|
+
adapter.remove @id.object_id
|
29
|
+
end
|
42
30
|
|
43
|
-
|
31
|
+
def remove_mapping
|
32
|
+
GOM::Object::Mapping.remove_by_object @object
|
33
|
+
end
|
44
34
|
|
35
|
+
def adapter
|
36
|
+
@adapter ||= GOM::Storage::Configuration[@id.storage_name].adapter
|
45
37
|
end
|
46
38
|
|
47
39
|
end
|
data/lib/gom/storage/saver.rb
CHANGED
@@ -1,57 +1,49 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
# Stores an object to the storage.
|
3
|
+
class GOM::Storage::Saver
|
3
4
|
|
4
|
-
|
5
|
+
attr_reader :object
|
6
|
+
attr_reader :storage_name
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attr_reader :object
|
10
|
-
attr_reader :storage_name
|
11
|
-
|
12
|
-
def initialize(object, storage_name = nil)
|
13
|
-
@object, @storage_name = object, storage_name
|
14
|
-
end
|
15
|
-
|
16
|
-
def perform
|
17
|
-
fetch_id
|
18
|
-
update_storage_name
|
19
|
-
inspect_object
|
20
|
-
store_draft
|
21
|
-
store_id
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
8
|
+
def initialize(object, storage_name = nil)
|
9
|
+
@object, @storage_name = object, storage_name
|
10
|
+
end
|
25
11
|
|
26
|
-
|
27
|
-
|
28
|
-
|
12
|
+
def perform
|
13
|
+
fetch_id
|
14
|
+
update_storage_name
|
15
|
+
inspect_object
|
16
|
+
store_draft
|
17
|
+
store_id
|
18
|
+
end
|
29
19
|
|
30
|
-
|
31
|
-
@storage_name ||= @id.storage_name if @id
|
32
|
-
@storage_name ||= GOM::Storage::Configuration.default.name
|
33
|
-
end
|
20
|
+
private
|
34
21
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
22
|
+
def fetch_id
|
23
|
+
@id = GOM::Object::Mapping.id_by_object @object
|
24
|
+
end
|
39
25
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
26
|
+
def update_storage_name
|
27
|
+
@storage_name ||= @id.storage_name if @id
|
28
|
+
@storage_name ||= GOM::Storage::Configuration.default.name
|
29
|
+
end
|
44
30
|
|
45
|
-
|
46
|
-
|
47
|
-
|
31
|
+
def inspect_object
|
32
|
+
@draft = GOM::Object::Inspector.new(@object).draft
|
33
|
+
@draft.id = @id.object_id if @id
|
34
|
+
end
|
48
35
|
|
49
|
-
|
50
|
-
|
51
|
-
|
36
|
+
def store_draft
|
37
|
+
object_id = adapter.store @draft
|
38
|
+
@id = GOM::Object::Id.new @storage_name.to_s, object_id
|
39
|
+
end
|
52
40
|
|
53
|
-
|
41
|
+
def store_id
|
42
|
+
GOM::Object::Mapping.put @object, @id
|
43
|
+
end
|
54
44
|
|
45
|
+
def adapter
|
46
|
+
GOM::Storage::Configuration[@storage_name].adapter
|
55
47
|
end
|
56
48
|
|
57
49
|
end
|
@@ -1,10 +1,22 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "gom"))
|
3
2
|
|
4
3
|
GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
|
5
4
|
|
6
|
-
describe "
|
5
|
+
describe "adapter" do
|
7
6
|
|
8
|
-
|
7
|
+
before :each do
|
8
|
+
GOM::Storage.teardown
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
describe "setup" do
|
12
|
+
|
13
|
+
it "should raise a #{GOM::Storage::AdapterNotFoundError} if no adapter of that name is registered" do
|
14
|
+
GOM::Storage::Configuration[:test_storage].hash[:adapter] = "invalid"
|
15
|
+
lambda do
|
16
|
+
GOM::Storage.setup
|
17
|
+
end.should raise_error(GOM::Storage::AdapterNotFoundError)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
|
2
|
+
|
3
|
+
GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
|
4
|
+
|
5
|
+
describe "fake adapter" do
|
6
|
+
|
7
|
+
it_should_behave_like "an adapter that needs setup"
|
8
|
+
|
9
|
+
it_should_behave_like "an adapter connected to a stateful storage"
|
10
|
+
|
11
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "gom"))
|
3
2
|
|
4
3
|
GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
|
5
4
|
|
@@ -10,6 +9,10 @@ describe "object" do
|
|
10
9
|
@object = Object.new
|
11
10
|
end
|
12
11
|
|
12
|
+
after :each do
|
13
|
+
GOM::Storage.teardown
|
14
|
+
end
|
15
|
+
|
13
16
|
describe "getting it's id" do
|
14
17
|
|
15
18
|
before :each do
|
data/spec/fake_adapter.rb
CHANGED
@@ -37,15 +37,22 @@ class FakeAdapter < GOM::Storage::Adapter
|
|
37
37
|
@store = { }
|
38
38
|
end
|
39
39
|
|
40
|
+
def teardown
|
41
|
+
@store = nil
|
42
|
+
end
|
43
|
+
|
40
44
|
def fetch(id)
|
45
|
+
raise GOM::Storage::Adapter::NoSetupError unless @store
|
41
46
|
@store[id]
|
42
47
|
end
|
43
48
|
|
44
49
|
def store(draft)
|
50
|
+
raise GOM::Storage::Adapter::NoSetupError unless @store
|
45
51
|
# store relations
|
46
52
|
draft.relations.each do |key, related_object_proxy|
|
47
|
-
|
48
|
-
|
53
|
+
related_object = related_object_proxy.object
|
54
|
+
GOM::Storage.store related_object, configuration.name
|
55
|
+
id = GOM::Object::Mapping.id_by_object related_object
|
49
56
|
draft.relations[key] = GOM::Object::Proxy.new id
|
50
57
|
end
|
51
58
|
|
@@ -59,13 +66,15 @@ class FakeAdapter < GOM::Storage::Adapter
|
|
59
66
|
end
|
60
67
|
|
61
68
|
def remove(id)
|
69
|
+
raise GOM::Storage::Adapter::NoSetupError unless @store
|
62
70
|
@store.delete id
|
63
71
|
end
|
64
72
|
|
65
73
|
def collection(view_name)
|
74
|
+
raise GOM::Storage::Adapter::NoSetupError unless @store
|
66
75
|
case view_name.to_sym
|
67
76
|
when :test_object_class_view
|
68
|
-
GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "Object")
|
77
|
+
GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "GOM::Spec::Object")
|
69
78
|
when :test_map_view
|
70
79
|
GOM::Object::Collection.new MapReduceCollectionFetcher.new(@store, configuration.name, :number, 11)
|
71
80
|
end
|
@@ -3,9 +3,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "sp
|
|
3
3
|
describe GOM::Object::Builder do
|
4
4
|
|
5
5
|
before :each do
|
6
|
-
@related_object_proxy = Object
|
6
|
+
@related_object_proxy = mock GOM::Object::Proxy
|
7
7
|
|
8
|
-
@draft = GOM::Object::Draft.new nil,
|
8
|
+
@draft = GOM::Object::Draft.new nil, GOM::Spec::Object.to_s, { :number => 21 }, { :related_object => @related_object_proxy }
|
9
9
|
@builder = described_class.new @draft
|
10
10
|
end
|
11
11
|
|
@@ -13,7 +13,7 @@ describe GOM::Object::Builder do
|
|
13
13
|
|
14
14
|
it "should initialize an object of the right class" do
|
15
15
|
object = @builder.object
|
16
|
-
object.class.should == Object
|
16
|
+
object.class.should == GOM::Spec::Object
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should not initialize an object if one is given" do
|
@@ -24,12 +24,12 @@ describe GOM::Object::Builder do
|
|
24
24
|
|
25
25
|
it "should set the properties" do
|
26
26
|
object = @builder.object
|
27
|
-
object.
|
27
|
+
object.number.should == 21
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should set the relations" do
|
31
31
|
object = @builder.object
|
32
|
-
object.
|
32
|
+
object.related_object.should == @related_object_proxy
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should work without a properties hash" do
|
@@ -3,24 +3,20 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "sp
|
|
3
3
|
describe GOM::Object::Inspector do
|
4
4
|
|
5
5
|
before :each do
|
6
|
-
@related_object_proxy = GOM::Object.reference Object.new
|
6
|
+
@related_object_proxy = GOM::Object.reference GOM::Spec::Object.new
|
7
7
|
|
8
|
-
@object = Object.new
|
9
|
-
@object.
|
10
|
-
@object.
|
8
|
+
@object = GOM::Spec::Object.new
|
9
|
+
@object.number = 12
|
10
|
+
@object.related_object = @related_object_proxy
|
11
11
|
|
12
12
|
@inspector = GOM::Object::Inspector.new @object
|
13
13
|
end
|
14
14
|
|
15
15
|
describe "draft" do
|
16
16
|
|
17
|
-
before :each do
|
18
|
-
@draft = GOM::Object::Draft.new nil, "Object", { :test => "test value" }, { :related_object => @related_object_proxy }
|
19
|
-
end
|
20
|
-
|
21
17
|
it "should return the correct draft" do
|
22
18
|
draft = @inspector.draft
|
23
|
-
draft.should == @
|
19
|
+
draft.should == GOM::Object::Draft.new(nil, GOM::Spec::Object.to_s, { :number => 12 }, { :related_object => @related_object_proxy })
|
24
20
|
end
|
25
21
|
|
26
22
|
end
|
@@ -60,6 +60,23 @@ describe GOM::Object::Mapping do
|
|
60
60
|
|
61
61
|
end
|
62
62
|
|
63
|
+
describe "size" do
|
64
|
+
|
65
|
+
it "should return the number of mapping entries" do
|
66
|
+
@mapping.size.should == 1
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "clear!" do
|
72
|
+
|
73
|
+
it "should remove all mappings" do
|
74
|
+
@mapping.clear!
|
75
|
+
@mapping.size.should == 0
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
63
80
|
end
|
64
81
|
|
65
82
|
describe "class method" do
|
@@ -88,67 +105,20 @@ describe GOM::Object::Mapping do
|
|
88
105
|
|
89
106
|
end
|
90
107
|
|
91
|
-
|
92
|
-
|
93
|
-
before :each do
|
94
|
-
GOM::Object::Mapping.stub!(:singleton).and_return(@mapping)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should call put on the mapping instance" do
|
98
|
-
@mapping.should_receive(:put).with(@object, @id)
|
99
|
-
GOM::Object::Mapping.put @object, @id
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
describe "object_by_id" do
|
105
|
-
|
106
|
-
before :each do
|
107
|
-
GOM::Object::Mapping.stub!(:singleton).and_return(@mapping)
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should call object_by_id on the mapping instance" do
|
111
|
-
@mapping.should_receive(:object_by_id).with(@id).and_return(@object)
|
112
|
-
GOM::Object::Mapping.object_by_id(@id).should == @object
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
describe "id_by_object" do
|
118
|
-
|
119
|
-
before :each do
|
120
|
-
GOM::Object::Mapping.stub!(:singleton).and_return(@mapping)
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should call id_by_object on the mapping instance" do
|
124
|
-
@mapping.should_receive(:id_by_object).with(@object).and_return(@id)
|
125
|
-
GOM::Object::Mapping.id_by_object(@object).should == @id
|
126
|
-
end
|
108
|
+
[ :put, :object_by_id, :id_by_object, :remove_by_id, :remove_by_object, :size ].each do |method_name|
|
127
109
|
|
128
|
-
|
110
|
+
describe "put" do
|
129
111
|
|
130
|
-
|
112
|
+
before :each do
|
113
|
+
GOM::Object::Mapping.stub(:singleton).and_return(@mapping)
|
114
|
+
end
|
131
115
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
@mapping.should_receive(:remove_by_id).with(@id)
|
138
|
-
GOM::Object::Mapping.remove_by_id(@id)
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
describe "remove_by_object" do
|
144
|
-
|
145
|
-
before :each do
|
146
|
-
GOM::Object::Mapping.stub!(:singleton).and_return(@mapping)
|
147
|
-
end
|
116
|
+
it "should call #{method_name} on the mapping instance" do
|
117
|
+
@mapping.should_receive(method_name).with(:argument).and_return(:result)
|
118
|
+
result = GOM::Object::Mapping.send method_name, :argument
|
119
|
+
result.should == :result
|
120
|
+
end
|
148
121
|
|
149
|
-
it "should call remove_by_object on the mapping instance" do
|
150
|
-
@mapping.should_receive(:remove_by_object).with(@object)
|
151
|
-
GOM::Object::Mapping.remove_by_object(@object)
|
152
122
|
end
|
153
123
|
|
154
124
|
end
|