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:
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/activefacts/api/entity.rb', line 457 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!
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 |
# File 'lib/activefacts/api/entity.rb', line 163 def analyse_impacts role impacts = [] # 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 << [supertype, self, old_key] end # Now consider objects whose identifiers include this object. # Find our roles in those identifiers first. impacted_roles = [] self.class.all_role_transitive.each do |n, role| if role.counterpart && role.counterpart. # puts "Changing #{role.inspect} affects #{role.inspect}" impacted_roles << role end end impacted_roles.each do |role| affected_instances = Array(instance_variable_get(role.variable)) # 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
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/activefacts/api/entity.rb', line 195 def apply_impacts impacts impacts.each do |klass, entity, old_key| instance_index = entity.constellation.instances[klass] new_key = entity.(klass) # puts "Reindexing #{klass} from #{old_key.inspect} to #{new_key.inspect}" if new_key != old_key instance_index.delete(old_key) instance_index[new_key] = entity 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/activefacts/api/entity.rb', line 210 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
114 115 116 117 118 119 120 |
# File 'lib/activefacts/api/entity.rb', line 114 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
103 104 105 106 107 108 109 110 |
# File 'lib/activefacts/api/entity.rb', line 103 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
133 134 135 136 137 138 139 |
# File 'lib/activefacts/api/entity.rb', line 133 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.
142 143 144 |
# File 'lib/activefacts/api/entity.rb', line 142 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.
150 151 152 153 154 155 156 |
# File 'lib/activefacts/api/entity.rb', line 150 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 99 |
# File 'lib/activefacts/api/entity.rb', line 93 def inspect #:nodoc: inc = constellation ? " in #{constellation.inspect}" : "" irnv = self.class..map do |role_name| "@#{role_name}="+send(role_name).inspect end "\#<#{self.class.basename}:#{object_id}#{inc} #{ irnv*' ' }>" end |
#verbalise(role_name = nil) ⇒ Object
Verbalise this entity instance
123 124 125 126 127 128 129 130 |
# File 'lib/activefacts/api/entity.rb', line 123 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 |