Module: ActiveFacts::API::Entity
- Includes:
- Instance
- Defined in:
- lib/activefacts/api/entity.rb
Overview
An Entity type is any ObjectType that isn’t a value type. All Entity types must have an identifier made up of one or more roles.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary
Attributes included from Instance
Class Method Summary collapse
-
.included(other) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#analyse_impacts(role) ⇒ Object
This role is identifying, so if is changed, not only must the current object be re-indexed, but also entities identified by this entity.
- #apply_impacts(impacts) ⇒ Object
-
#check_identification_change_legality(role, value) ⇒ Object
If this instance’s role is updated to the new value, does that cause a collision? We need to check each superclass that has a different identification pattern.
-
#eql?(other) ⇒ Boolean
When used as a hash key, this entity instance is compared with another by comparing the values of its identifying roles.
-
#hash ⇒ Object
When used as a hash key, the hash key of this entity instance is calculated by hashing the values of its identifying roles.
-
#identifying_role_values(klass = self.class) ⇒ Object
Return the array of the values of this instance’s identifying roles.
-
#identity_as_hash ⇒ Object
Identifying role values in a hash form.
-
#identity_by(klass) ⇒ Object
Identifying role values in a hash form by class (entity).
-
#inspect ⇒ Object
:nodoc:.
-
#verbalise(role_name = nil) ⇒ Object
Verbalise this entity instance.
Methods included from Instance
#instance_index, #is_a?, #related_entities, #retract
Class Method Details
.included(other) ⇒ Object
:nodoc:
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/activefacts/api/entity.rb', line 463 def self.included other #:nodoc: other.send :extend, ClassMethods def other.new_instance constellation, *args instance = allocate instance.instance_variable_set(@@constellation_variable_name ||= "@constellation", constellation) instance.send(:initialize, *args) instance end # Register ourselves with the parent module, which has become a Vocabulary: vocabulary = other.modspace unless vocabulary.respond_to? :object_type # Extend module with Vocabulary if necessary vocabulary.send :extend, Vocabulary end vocabulary.__add_object_type(other) end |
Instance Method Details
#analyse_impacts(role) ⇒ Object
This role is identifying, so if is changed, not only must the current object be re-indexed, but also entities identified by this entity. Save the current key and class for each such instance. This function is transitive!
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/activefacts/api/entity.rb', line 162 def analyse_impacts role impacts = [] impacted_roles = [] # Consider the object itself and all its supertypes ([self.class]+self.class.supertypes_transitive).map do |supertype| next unless supertype..include?(role) old_key = (supertype) # puts "Need to reindex #{self.class} as #{supertype} from #{old_key.inspect}" impacts << [constellation.instances[supertype], self, old_key] supertype. all_role. each do |role_name, propagation_role| next if role == propagation_role # Propagation has already been taken care of next unless counterpart = propagation_role.counterpart # And the role is not unary if counterpart. # This object identifies another # puts "Changing #{propagation_role.inspect} affects #{counterpart.inspect}" impacted_roles << propagation_role else next if counterpart.unique # But a one-to-many next unless value = send(propagation_role.getter) # A value is set role_values = value.send(counterpart.getter) # This is the index we have to change # puts "Changing #{role.inspect} of a #{self.class} requires updating #{propagation_role.counterpart.inspect}" impacts << [role_values, self, old_key] end end end impacted_roles.each do |role| affected_instances = Array(send(role.getter)) # puts "considering #{affected_instances.size} #{role.object_type.name} instances that include #{role.inspect}: #{affected_instances.map(&:identifying_role_values).inspect}" affected_instances.each do |counterpart| impacts.concat(counterpart.analyse_impacts(role.counterpart)) end end impacts end |
#apply_impacts(impacts) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/activefacts/api/entity.rb', line 202 def apply_impacts impacts impacts.each do |index, entity, old_key| new_key = entity.(index.object_type) # puts "Reindexing #{klass} from #{old_key.inspect} to #{new_key.inspect}" if new_key != old_key index.delete_instance(entity, old_key) index.add_instance(entity, new_key) end end end |
#check_identification_change_legality(role, value) ⇒ Object
If this instance’s role is updated to the new value, does that cause a collision? We need to check each superclass that has a different identification pattern
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/activefacts/api/entity.rb', line 216 def check_identification_change_legality(role, value) return unless @constellation && role. klasses = [self.class] + self.class.supertypes_transitive last_identity = nil last_irns = nil counterpart_class = role.counterpart ? role.counterpart.object_type : value.class duplicate = klasses.detect do |klass| next false unless klass..include?(role) irns = klass. if last_irns != irns last_identity = (klass) role_position = irns.index(role.name) last_identity[role_position] = value.(counterpart_class) end @constellation.instances[klass][last_identity] end raise DuplicateIdentifyingValueException.new(self.class, role.name, value) if duplicate end |
#eql?(other) ⇒ Boolean
When used as a hash key, this entity instance is compared with another by comparing the values of its identifying roles
113 114 115 116 117 118 119 |
# File 'lib/activefacts/api/entity.rb', line 113 def eql?(other) if self.class == other.class identity_as_hash == other.identity_as_hash else false end end |
#hash ⇒ Object
When used as a hash key, the hash key of this entity instance is calculated by hashing the values of its identifying roles
102 103 104 105 106 107 108 109 |
# File 'lib/activefacts/api/entity.rb', line 102 def hash self.class..map{|role| instance_variable_get(role.variable) }.inject(0) { |h,v| h ^= v.hash h } end |
#identifying_role_values(klass = self.class) ⇒ Object
Return the array of the values of this instance’s identifying roles
132 133 134 135 136 137 138 |
# File 'lib/activefacts/api/entity.rb', line 132 def (klass = self.class) klass..map do |role| value = send(role.name) counterpart_class = role.counterpart && role.counterpart.object_type value.(counterpart_class) end end |
#identity_as_hash ⇒ Object
Identifying role values in a hash form.
141 142 143 |
# File 'lib/activefacts/api/entity.rb', line 141 def identity_as_hash identity_by(self.class) end |
#identity_by(klass) ⇒ Object
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 may be identified differently if compared to one of its supertype.
149 150 151 152 153 154 155 |
# File 'lib/activefacts/api/entity.rb', line 149 def identity_by(klass) roles_hash = {} klass..each do |role| roles_hash[role.getter] = send(role.getter) end roles_hash end |
#inspect ⇒ Object
:nodoc:
93 94 95 96 97 98 |
# File 'lib/activefacts/api/entity.rb', line 93 def inspect #:nodoc: irnv = self.class..map do |role_name| "#{role_name}: "+send(role_name).inspect end "<#{self.class.name} #{ irnv*', ' }>" end |
#verbalise(role_name = nil) ⇒ Object
Verbalise this entity instance
122 123 124 125 126 127 128 129 |
# File 'lib/activefacts/api/entity.rb', line 122 def verbalise(role_name = nil) irnv = self.class..map do |role_sym| value = send(role_sym) = self.class.all_role(role_sym).name.to_s.camelcase value ? value.verbalise() : "nil" end "#{role_name || self.class.basename}(#{ irnv*', ' })" end |