lib/activefacts/api/instance.rb in activefacts-api-0.9.9 vs lib/activefacts/api/instance.rb in activefacts-api-1.0.0
- old
+ new
@@ -55,11 +55,11 @@
end
# List entities which have an identifying role played by this object.
def related_entities(indirectly = true, instances = [])
# Check all roles of this instance
- self.class.roles.each do |role_name, role|
+ self.class.all_role.each do |role_name, role|
# If the counterpart role is not identifying for its object type, skip it
next unless c = role.counterpart and c.is_identifying
identified_instances = Array(self.send(role.getter))
instances.concat(identified_instances)
@@ -81,26 +81,29 @@
# Now, for all roles (from this class and all supertypes), assign nil to all functional roles
# The counterpart roles get cleared automatically.
klasses = [self.class]+self.class.supertypes_transitive
- irvks = {} # identifying_role_values by class
- klasses.each do |klass|
- if !irvks[klass] and klass.roles.detect{|_, role| role.counterpart and !role.counterpart.unique and send(role.getter) }
- # We will need the identifying_role_values for this role's object_type
- irvks[klass] = identifying_role_values(klass)
- end
+ irvrvs = {} # identifying_role_values by RoleValues
+ self.class.all_role_transitive.each do |_, role|
+ next unless role.counterpart and
+ role.unique and
+ !role.counterpart.unique and
+ counterpart = send(role.getter)
+ role_values = counterpart.send(role.counterpart.getter)
+ irvrvs[role_values] = role_values.index_values(self)
end
klasses.each do |klass|
- klass.roles.each do |role_name, role|
+ klass.all_role.each do |role_name, role|
next if role.unary?
counterpart = role.counterpart
# Objects being created do not have to have non-identifying mandatory roles,
# so we allow retracting to the same state.
if role.unique
+ next if role.fact_type.is_a?(TypeInheritanceFactType)
i = send(role.getter)
next unless i
if counterpart.is_identifying && counterpart.mandatory
# We play a mandatory identifying role in i; so retract that (it'll clear our instance variable)
i.retract
@@ -108,16 +111,17 @@
if (counterpart.unique)
# REVISIT: This will incorrectly fail to propagate a key change for a non-mandatory role
i.send(counterpart.setter, nil, false)
else
rv = i.send(role.counterpart.getter)
- rv.delete_instance(self, irvks[role.object_type])
+ rv.delete_instance(self, irvrvs[rv])
end
end
instance_variable_set(role.variable, nil)
else
# puts "Not removing role #{role_name} from counterpart RoleValues #{counterpart.name}"
# Duplicate the array using to_a, as the RoleValues here will be modified as we traverse it:
+ next if role.fact_type.is_a?(TypeInheritanceFactType)
counterpart_instances = send(role.name)
counterpart_instances.to_a.each do |counterpart_instance|
# These actions deconstruct the RoleValues as we go:
if counterpart.is_identifying && counterpart.mandatory
counterpart_instance.retract