Permalink
Browse files

Fix pending spec (clone across constellation).

  • Loading branch information...
1 parent f7c73d1 commit 26aca9874f0f7baff132a6fcc5b87704c8d353b5 @kouno committed Aug 3, 2012
@@ -66,6 +66,11 @@ def retract(*instances)
self
end
+ # Copies object type across constellations.
+ def copy(value)
+ send(value.class.basename, value.clone_identity)
+ end
+
=begin
def assert *args
case
@@ -114,6 +114,16 @@ def identity_as_hash
identity_by(self.class)
end
+ # Clones identity.
+ #
+ # Cloning an entity identity means copying its class identifying values and also its supertypes identifying
+ # values.
+ def clone_identity
+ self.class.supertypes_transitive.inject(identity_as_hash) do |roles_hash, supertype|
+ roles_hash.merge!(identity_by(supertype))
+ end
+ end
+
# Identifying role values in a hash form by class (entity).
#
# Subtypes may have different identifying roles compared to their supertype, and therefore, a subtype entity
@@ -74,9 +74,7 @@ def adapt(constellation, value) #:nodoc:
if value.is_a?(counterpart.object_type)
# Check that the value is in a compatible constellation, clone if not:
if constellation && (vc = value.constellation) && vc != constellation
- # Cross-constellation assignment!
- # Just take the identifying_role_values to make a new object
- value = constellation.send(value.class.basename, value.identifying_role_values)
+ value = constellation.copy(value)
end
value.constellation = constellation if constellation
else
@@ -30,6 +30,10 @@ def verbalise(role_name = nil)
"#{role_name || self.class.basename} '#{to_s}'"
end
+ def clone_identity
+ identifying_role_values
+ end
+
# A value is its own key, unless it's a delegate for a raw value
def identifying_role_values #:nodoc:
__getobj__ rescue self
@@ -177,21 +177,19 @@ def foo
end
it "should be able to import an entity from another constellation which subclass another entity" do
- pending "fails because identify_role_values get only the current class identifying roles" do
- # in this example, it returns :identifier, but not :name from LegalEntity
- module Mod
- class Person2 < LegalEntity
- identified_by :identifier
- one_to_one :identifier
- end
+ # in this example, it returns :identifier, but not :name from LegalEntity
+ module Mod
+ class Person2 < LegalEntity
+ identified_by :identifier
+ one_to_one :identifier
end
+ end
- c1 = ActiveFacts::API::Constellation.new(Mod)
- c2 = ActiveFacts::API::Constellation.new(Mod)
+ c1 = ActiveFacts::API::Constellation.new(Mod)
+ c2 = ActiveFacts::API::Constellation.new(Mod)
- p = c1.Person2("Person2Name", :identifier => "Project2501")
- identifier = c2.Identifier("Project2501", :person2 => p)
- identifier.person2.name.should == "Person2Name"
- end
+ p = c1.Person2(:name => "Person2Name", :identifier => "Project2501")
+ identifier = c2.Identifier("Project2501", :person2 => p)
+ identifier.person2.name.should == "Person2Name"
end
end

0 comments on commit 26aca98

Please sign in to comment.