Permalink
Browse files

clears mapping during setup now

can handle classnames nested in modules
gom-13: Add support for classes that are nested in modules.
  • Loading branch information...
1 parent 18d55ee commit ef11ece4b98589755c3ac412f936ebe58d58a63c @phifty committed Jan 25, 2011
View
@@ -2,8 +2,8 @@
Gem::Specification.new do |specification|
specification.name = "gom"
- specification.version = "0.2.0"
- specification.date = "2010-12-20"
+ specification.version = "0.2.1"
+ specification.date = "2011-01-25"
specification.authors = [ "Philipp Brüll" ]
specification.email = "b.phifty@gmail.com"
View
@@ -2,6 +2,7 @@
module GOM
autoload :Object, File.join(File.dirname(__FILE__), "gom", "object")
+ autoload :Spec, File.join(File.dirname(__FILE__), "gom", "spec")
autoload :Storage, File.join(File.dirname(__FILE__), "gom", "storage")
end
View
@@ -23,9 +23,19 @@ def object
private
def initialize_object
- klass = Object.const_get @draft.class_name
- arity = [ klass.method(:new).arity, 0 ].max
- @object = klass.new *([ nil ] * arity)
+ set_class
+ arity = [ @klass.method(:new).arity, 0 ].max
+ @object = @klass.new *([ nil ] * arity)
+ end
+
+ def set_class
+ names = @draft.class_name.split "::"
+ names.shift if names.empty? || names.first.empty?
+
+ @klass = Object
+ names.each do |name|
+ @klass = @klass.const_defined?(name) ? @klass.const_get(name) : @klass.const_missing(name)
+ end
end
def set_properties
View
@@ -30,28 +30,20 @@ def remove_by_object(object)
@map.delete object
end
- def self.singleton
- @mapping ||= self.new
- end
-
- def self.put(object, id)
- self.singleton.put object, id
+ def size
+ @map.size
end
- def self.object_by_id(id)
- self.singleton.object_by_id id
+ def clear!
+ @map.clear
end
- def self.id_by_object(object)
- self.singleton.id_by_object object
- end
-
- def self.remove_by_id(id)
- self.singleton.remove_by_id id
+ def self.singleton
+ @mapping ||= self.new
end
- def self.remove_by_object(object)
- self.singleton.remove_by_object object
+ def self.method_missing(method_name, *arguments, &block)
+ self.singleton.send method_name, *arguments, &block
end
end
View
@@ -2,3 +2,13 @@
require File.join(File.dirname(__FILE__), "spec", "acceptance", "adapter_with_stateful_storage")
require File.join(File.dirname(__FILE__), "spec", "acceptance", "read_only_adapter_with_stateless_storage")
+
+module GOM
+
+ module Spec
+
+ autoload :Object, File.join(File.dirname(__FILE__), "spec", "object")
+
+ end
+
+end
@@ -6,19 +6,19 @@
end
before :each do
- @related_object = Object.new
- @related_object.instance_variable_set :@number, 16
+ @related_object = GOM::Spec::Object.new
+ @related_object.number = 16
- @object = Object.new
- @object.instance_variable_set :@number, 11
- @object.instance_variable_set :@related_object, GOM::Object.reference(@related_object)
+ @object = GOM::Spec::Object.new
+ @object.number = 11
+ @object.related_object = GOM::Object.reference @related_object
end
describe "fetching an object" do
before :each do
GOM::Storage.store @object, :test_storage
- @id = GOM::Object.id(@object)
+ @id = GOM::Object.id @object
@object = GOM::Storage.fetch @id
end
@@ -29,21 +29,23 @@
end
it "should return an object of the correct class" do
- @object.class.should == Object
+ @object.class.should == GOM::Spec::Object
end
it "should set the object's instance variables" do
- @object.instance_variable_get(:@number).should == 11
+ @object.number.should == 11
end
it "should assign an object id" do
GOM::Object.id(@object).should == @id
end
it "should also fetch the related object" do
- related_object_proxy = @object.instance_variable_get :@related_object
+ related_object_proxy = @object.related_object
related_object_proxy.should be_instance_of(GOM::Object::Proxy)
- related_object_proxy.object.instance_variable_get(:@number).should == 16
+ # draft = GOM::Storage::Configuration.default.adapter.fetch related_object_proxy.id.object_id
+ # puts "DRAFT ::: " + draft.inspect
+ related_object_proxy.object.number.should == 16
end
end
@@ -74,7 +76,7 @@
it "should store the related object" do
GOM::Storage.store @object, :test_storage
related_object = GOM::Storage.fetch GOM::Object.id(@related_object)
- related_object.instance_variable_get(:@number).should == 16
+ related_object.number.should == 16
end
end
@@ -115,7 +117,7 @@
describe "fetching a class collection" do
before :each do
- @another_object = mock Object, :class => mock(Class, :to_s => "Test")
+ @another_object = Object.new
@another_object.instance_variable_set :@number, 17
GOM::Storage.store @object, :test_storage
@@ -138,13 +140,8 @@
collection.size.should > 0
collection.each do |object_proxy|
object_proxy.should be_instance_of(GOM::Object::Proxy)
- [
- @object.instance_variable_get(:@number),
- @related_object.instance_variable_get(:@number)
- ].should include(object_proxy.object.instance_variable_get(:@number))
- [
- @another_object.instance_variable_get(:@number)
- ].should_not include(object_proxy.object.instance_variable_get(:@number))
+ [ @object.number, @related_object.number ].should include(object_proxy.number)
+ [ @another_object.instance_variable_get(:@number) ].should_not include(object_proxy.instance_variable_get(:@number))
end
end
@@ -171,7 +168,7 @@
collection.size.should > 0
collection.each do |object_proxy|
object_proxy.should be_instance_of(GOM::Object::Proxy)
- object_proxy.object.instance_variable_get(:@number).should == @object.instance_variable_get(:@number)
+ object_proxy.object.number.should == @object.number
end
end
@@ -9,16 +9,16 @@
it "should return the correct object" do
object = GOM::Storage.fetch "test_storage:object_1"
- object.should be_instance_of(Object)
- object.instance_variable_get(:@number).should == 5
+ object.should be_instance_of(GOM::Spec::Object)
+ object.number.should == 5
GOM::Object.id(object).should == "test_storage:object_1"
end
it "should return proxy objects that fetches the related objects" do
object = GOM::Storage.fetch "test_storage:object_1"
- related_object = object.instance_variable_get :@related_object
+ related_object = object.related_object
related_object.should be_instance_of(GOM::Object::Proxy)
- related_object.object.instance_variable_get(:@test).should == "test value"
+ related_object.object.number.should == 7
end
end
@@ -58,10 +58,7 @@
collection = GOM::Storage.collection :test_storage, :test_object_class_view
collection.size > 0
collection.each do |object_proxy|
- (
- object_proxy.object.instance_variable_get(:@number) == 5 ||
- object_proxy.object.instance_variable_get(:@test) == "test value"
- ).should be_true
+ [ 5, 7 ].should include(object_proxy.object.number)
end
end
View
@@ -0,0 +1,16 @@
+
+module GOM
+
+ module Spec
+
+ # A dummy test class
+ class Object
+
+ attr_accessor :number
+ attr_accessor :related_object
+
+ end
+
+ end
+
+end
View
@@ -13,6 +13,7 @@ module Storage
class ReadOnlyError < StandardError; end
def self.setup
+ GOM::Object::Mapping.clear!
Configuration.setup_all
end
View
@@ -44,8 +44,9 @@ def fetch(id)
def store(draft)
# store relations
draft.relations.each do |key, related_object_proxy|
- GOM::Storage.store related_object_proxy.object, configuration.name
- id = GOM::Object::Mapping.id_by_object related_object_proxy.object
+ related_object = related_object_proxy.object
+ GOM::Storage.store related_object, configuration.name
+ id = GOM::Object::Mapping.id_by_object related_object
draft.relations[key] = GOM::Object::Proxy.new id
end
@@ -65,7 +66,7 @@ def remove(id)
def collection(view_name)
case view_name.to_sym
when :test_object_class_view
- GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "Object")
+ GOM::Object::Collection.new ClassCollectionFetcher.new(@store, configuration.name, "GOM::Spec::Object")
when :test_map_view
GOM::Object::Collection.new MapReduceCollectionFetcher.new(@store, configuration.name, :number, 11)
end
@@ -3,17 +3,17 @@
describe GOM::Object::Builder do
before :each do
- @related_object_proxy = Object.new
+ @related_object_proxy = mock GOM::Object::Proxy
- @draft = GOM::Object::Draft.new nil, "Object", { :test => "test value" }, { :related_object => @related_object_proxy }
+ @draft = GOM::Object::Draft.new nil, GOM::Spec::Object.to_s, { :number => 21 }, { :related_object => @related_object_proxy }
@builder = described_class.new @draft
end
describe "object" do
it "should initialize an object of the right class" do
object = @builder.object
- object.class.should == Object
+ object.class.should == GOM::Spec::Object
end
it "should not initialize an object if one is given" do
@@ -24,12 +24,12 @@
it "should set the properties" do
object = @builder.object
- object.instance_variable_get(:@test).should == "test value"
+ object.number.should == 21
end
it "should set the relations" do
object = @builder.object
- object.instance_variable_get(:@related_object).should == @related_object_proxy
+ object.related_object.should == @related_object_proxy
end
it "should work without a properties hash" do
@@ -3,24 +3,20 @@
describe GOM::Object::Inspector do
before :each do
- @related_object_proxy = GOM::Object.reference Object.new
+ @related_object_proxy = GOM::Object.reference GOM::Spec::Object.new
- @object = Object.new
- @object.instance_variable_set :@test, "test value"
- @object.instance_variable_set :@related_object, @related_object_proxy
+ @object = GOM::Spec::Object.new
+ @object.number = 12
+ @object.related_object = @related_object_proxy
@inspector = GOM::Object::Inspector.new @object
end
describe "draft" do
- before :each do
- @draft = GOM::Object::Draft.new nil, "Object", { :test => "test value" }, { :related_object => @related_object_proxy }
- end
-
it "should return the correct draft" do
draft = @inspector.draft
- draft.should == @draft
+ draft.should == GOM::Object::Draft.new(nil, GOM::Spec::Object.to_s, { :number => 12 }, { :related_object => @related_object_proxy })
end
end
Oops, something went wrong.

0 comments on commit ef11ece

Please sign in to comment.