gom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +111 -0
  3. data/Rakefile +48 -0
  4. data/lib/gom.rb +7 -0
  5. data/lib/gom/object.rb +23 -0
  6. data/lib/gom/object/id.rb +30 -0
  7. data/lib/gom/object/injector.rb +45 -0
  8. data/lib/gom/object/inspector.rb +55 -0
  9. data/lib/gom/object/mapping.rb +61 -0
  10. data/lib/gom/object/proxy.rb +44 -0
  11. data/lib/gom/spec.rb +4 -0
  12. data/lib/gom/spec/acceptance/adapter_with_stateful_storage.rb +111 -0
  13. data/lib/gom/spec/acceptance/read_only_adapter_with_stateless_storage.rb +50 -0
  14. data/lib/gom/storage.rb +35 -0
  15. data/lib/gom/storage/adapter.rb +51 -0
  16. data/lib/gom/storage/configuration.rb +65 -0
  17. data/lib/gom/storage/fetcher.rb +69 -0
  18. data/lib/gom/storage/remover.rb +47 -0
  19. data/lib/gom/storage/saver.rb +59 -0
  20. data/spec/acceptance/adapter_spec.rb +10 -0
  21. data/spec/acceptance/object_spec.rb +33 -0
  22. data/spec/fake_adapter.rb +37 -0
  23. data/spec/lib/gom/object/id_spec.rb +56 -0
  24. data/spec/lib/gom/object/injector_spec.rb +51 -0
  25. data/spec/lib/gom/object/inspector_spec.rb +30 -0
  26. data/spec/lib/gom/object/mapping_spec.rb +158 -0
  27. data/spec/lib/gom/object/proxy_spec.rb +91 -0
  28. data/spec/lib/gom/object_spec.rb +40 -0
  29. data/spec/lib/gom/storage/adapter_spec.rb +73 -0
  30. data/spec/lib/gom/storage/configuration_spec.rb +92 -0
  31. data/spec/lib/gom/storage/fetcher_spec.rb +89 -0
  32. data/spec/lib/gom/storage/remover_spec.rb +47 -0
  33. data/spec/lib/gom/storage/saver_spec.rb +86 -0
  34. data/spec/lib/gom/storage_spec.rb +106 -0
  35. data/spec/spec_helper.rb +7 -0
  36. data/spec/storage.configuration +4 -0
  37. metadata +138 -0
@@ -0,0 +1,10 @@
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
+
4
+ GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
5
+
6
+ describe "fake adapter" do
7
+
8
+ it_should_behave_like "an adapter connected to a stateful storage"
9
+
10
+ end
@@ -0,0 +1,33 @@
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
+
4
+ GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
5
+
6
+ describe "object" do
7
+
8
+ before :each do
9
+ @object = Object.new
10
+ end
11
+
12
+ describe "getting it's id" do
13
+
14
+ before :each do
15
+ GOM::Storage.store @object, "test_storage"
16
+ end
17
+
18
+ it "should return the id" do
19
+ id = GOM::Object.id @object
20
+ id.should == "test_storage:object_1"
21
+ end
22
+
23
+ end
24
+
25
+ describe "getting an object reference" do
26
+
27
+ it "should create an object proxy" do
28
+ GOM::Object.reference(@object).should be_instance_of(GOM::Object::Proxy)
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,37 @@
1
+
2
+ class FakeAdapter < GOM::Storage::Adapter
3
+
4
+ STORE = { }
5
+
6
+ def setup
7
+
8
+ end
9
+
10
+ def fetch(id)
11
+ STORE[id]
12
+ end
13
+
14
+ def store(object_hash)
15
+ # store relations
16
+ (object_hash[:relations] || { }).each do |key, related_object_proxy|
17
+ GOM::Storage.store related_object_proxy.object, configuration.name
18
+ id = GOM::Object::Mapping.id_by_object related_object_proxy.object
19
+ object_hash[:relations][key] = GOM::Object::Proxy.new id
20
+ end
21
+
22
+ # may generate an id
23
+ object_id = object_hash[:id] || "object_#{STORE.size + 1}"
24
+
25
+ # store the object hash
26
+ STORE[object_id] = object_hash
27
+
28
+ object_id
29
+ end
30
+
31
+ def remove(id)
32
+ STORE.delete id
33
+ end
34
+
35
+ end
36
+
37
+ GOM::Storage::Adapter.register :fake_adapter, FakeAdapter
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Object::Id do
4
+
5
+ before :each do
6
+ @id = GOM::Object::Id.new "test_storage", "object_1"
7
+ end
8
+
9
+ describe "initialize" do
10
+
11
+ it "should initialize a blank id" do
12
+ id = GOM::Object::Id.new
13
+ id.storage_name.should == nil
14
+ id.object_id.should == nil
15
+ end
16
+
17
+ it "should initialize with an id string" do
18
+ id = GOM::Object::Id.new "test_storage:object_1"
19
+ id.storage_name.should == "test_storage"
20
+ id.object_id.should == "object_1"
21
+ end
22
+
23
+ it "should initialize with separated parameters" do
24
+ id = GOM::Object::Id.new "test_storage", "object_1"
25
+ id.storage_name.should == "test_storage"
26
+ id.object_id.should == "object_1"
27
+ end
28
+
29
+ end
30
+
31
+ describe "==" do
32
+
33
+ before :each do
34
+ @id_one = GOM::Object::Id.new "test_storage", "object_1"
35
+ @id_two = GOM::Object::Id.new "test_storage", "object_2"
36
+ end
37
+
38
+ it "should be true when compared to an id with the same parameters" do
39
+ (@id == @id_one).should be_true
40
+ end
41
+
42
+ it "should be false when compared to a different id" do
43
+ (@id == @id_two).should be_false
44
+ end
45
+
46
+ end
47
+
48
+ describe "to_s" do
49
+
50
+ it "should return the combined parameters" do
51
+ @id.to_s.should == "test_storage:object_1"
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Object::Injector do
4
+
5
+ before :each do
6
+ @related_object_proxy = Object.new
7
+
8
+ @object = Object.new
9
+ @object.instance_variable_set :@old, "old value"
10
+ @object_hash = {
11
+ :class => "Object",
12
+ :properties => { :test => "test value" },
13
+ :relations => { :related_object => @related_object_proxy }
14
+ }
15
+ @injector = GOM::Object::Injector.new @object, @object_hash
16
+ end
17
+
18
+ describe "perform" do
19
+
20
+ it "should clear all instance variables" do
21
+ @injector.perform
22
+ @injector.object.instance_variables.should_not include(:@old)
23
+ end
24
+
25
+ it "should set the properties" do
26
+ @injector.perform
27
+ @injector.object.instance_variable_get(:@test).should == "test value"
28
+ end
29
+
30
+ it "should set the relations" do
31
+ @injector.perform
32
+ @injector.object.instance_variable_get(:@related_object).should == @related_object_proxy
33
+ end
34
+
35
+ it "should work without a properties hash" do
36
+ @object_hash.delete :properties
37
+ lambda do
38
+ @injector.perform
39
+ end.should_not raise_error
40
+ end
41
+
42
+ it "should work without a relations hash" do
43
+ @object_hash.delete :relations
44
+ lambda do
45
+ @injector.perform
46
+ end.should_not raise_error
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Object::Inspector do
4
+
5
+ before :each do
6
+ @related_object_proxy = GOM::Object.reference Object.new
7
+
8
+ @object = Object.new
9
+ @object.instance_variable_set :@test, "test value"
10
+ @object.instance_variable_set :@related_object, @related_object_proxy
11
+
12
+ @object_hash = {
13
+ :class => "Object",
14
+ :properties => { :test => "test value" },
15
+ :relations => { :related_object => @related_object_proxy }
16
+ }
17
+
18
+ @inspector = GOM::Object::Inspector.new @object
19
+ end
20
+
21
+ describe "perform" do
22
+
23
+ it "should return the correct object hash" do
24
+ @inspector.perform
25
+ @inspector.object_hash.should == @object_hash
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,158 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Object::Mapping do
4
+
5
+ before :each do
6
+ @object = Object.new
7
+ @id = GOM::Object::Id.new "test_storage", "object_1"
8
+ @mapping = GOM::Object::Mapping.new
9
+ end
10
+
11
+ describe "put" do
12
+
13
+ it "should store a mapping between an object and an id" do
14
+ @mapping.put @object, @id
15
+ @mapping.object_by_id(@id).should == @object
16
+ end
17
+
18
+ end
19
+
20
+ describe "with a mapping" do
21
+
22
+ before :each do
23
+ @same_id = GOM::Object::Id.new "test_storage", "object_1"
24
+ @mapping.put @object, @id
25
+ end
26
+
27
+ describe "object_by_id" do
28
+
29
+ it "should return the object to the given id" do
30
+ @mapping.object_by_id(@id).should == @object
31
+ @mapping.object_by_id(@same_id).should == @object
32
+ end
33
+
34
+ end
35
+
36
+ describe "id_by_object" do
37
+
38
+ it "should return the id to the given object" do
39
+ @mapping.id_by_object(@object).should == @id
40
+ @mapping.id_by_object(@object).should == @same_id
41
+ end
42
+
43
+ end
44
+
45
+ describe "remove_by_id" do
46
+
47
+ it "should remove the mapping identified by id" do
48
+ @mapping.remove_by_id @id
49
+ @mapping.object_by_id(@id).should be_nil
50
+ end
51
+
52
+ end
53
+
54
+ describe "remove_by_object" do
55
+
56
+ it "should remove the mapping identified by object" do
57
+ @mapping.remove_by_object @object
58
+ @mapping.id_by_object(@object).should be_nil
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ describe "class method" do
66
+
67
+ before :each do
68
+ @object = Object.new
69
+ @id = GOM::Object::Id.new "test_storage", "object_1"
70
+
71
+ @mapping = Object.new
72
+ @mapping.stub!(:put)
73
+ @mapping.stub!(:object_by_id).and_return(@object)
74
+ @mapping.stub!(:id_by_object).and_return(@id)
75
+ end
76
+
77
+ describe "singleton" do
78
+
79
+ before :each do
80
+ GOM::Object::Mapping.stub!(:new).and_return(@mapping)
81
+ end
82
+
83
+ it "should initialize just one mapping instance" do
84
+ GOM::Object::Mapping.should_receive(:new).once.and_return(@mapping)
85
+ GOM::Object::Mapping.singleton
86
+ GOM::Object::Mapping.singleton
87
+ end
88
+
89
+ end
90
+
91
+ describe "put" do
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
127
+
128
+ end
129
+
130
+ describe "remove_by_id" do
131
+
132
+ before :each do
133
+ GOM::Object::Mapping.stub!(:singleton).and_return(@mapping)
134
+ end
135
+
136
+ it "should call remove_by_id on the mapping instance" do
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
148
+
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
+ end
153
+
154
+ end
155
+
156
+ end
157
+
158
+ end
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
2
+
3
+ describe GOM::Object::Proxy do
4
+
5
+ describe "initialized with an object" do
6
+
7
+ before :each do
8
+ @object = mock Object, :test_method => nil
9
+
10
+ @proxy = GOM::Object::Proxy.new @object
11
+ end
12
+
13
+ it "should pass every call to the object" do
14
+ @object.should_receive(:test_method).with(:test_argument).and_return(:test_result)
15
+ @proxy.test_method(:test_argument).should == :test_result
16
+ end
17
+
18
+ describe "object" do
19
+
20
+ it "should return the original object" do
21
+ @proxy.object.should == @object
22
+ end
23
+
24
+ end
25
+
26
+ describe "id" do
27
+
28
+ before :each do
29
+ @id = mock GOM::Object::Id
30
+ GOM::Object::Mapping.stub(:id_by_object).and_return(@id)
31
+ end
32
+
33
+ it "should fetch the id from the mapping" do
34
+ GOM::Object::Mapping.should_receive(:id_by_object).with(@object).and_return(@id)
35
+ @proxy.id
36
+ end
37
+
38
+ it "should return the fetched id" do
39
+ @proxy.id.should == @id
40
+ end
41
+
42
+ it "should return nil if no mapping exists" do
43
+ GOM::Object::Mapping.stub(:id_by_object).and_return(nil)
44
+ @proxy.id.should be_nil
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ describe "initialized with an id" do
52
+
53
+ before :each do
54
+ @id = GOM::Object::Id.new "test_storage", "object_1"
55
+
56
+ @object = mock Object, :test_method => nil
57
+
58
+ @fetcher = mock GOM::Storage::Fetcher, :object => @object
59
+ GOM::Storage::Fetcher.stub(:new).and_return(@fetcher)
60
+
61
+ @proxy = GOM::Object::Proxy.new @id
62
+ end
63
+
64
+ it "should fetch the object before the call is passed to it" do
65
+ GOM::Storage::Fetcher.should_receive(:new).with(@id).and_return(@fetcher)
66
+ @fetcher.should_receive(:object).and_return(@object)
67
+ @object.should_receive(:test_method).with(:test_argument).and_return(:test_result)
68
+ @proxy.test_method(:test_argument).should == :test_result
69
+ end
70
+
71
+ describe "object" do
72
+
73
+ it "should return the original object" do
74
+ GOM::Storage::Fetcher.should_receive(:new).with(@id).and_return(@fetcher)
75
+ @fetcher.should_receive(:object).and_return(@object)
76
+ @proxy.object.should == @object
77
+ end
78
+
79
+ end
80
+
81
+ describe "id" do
82
+
83
+ it "should return the id" do
84
+ @proxy.id.should == @id
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end