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
-
#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
#check_identification_change_legality, #instance_index, #is_a?, #related_entities, #retract
Class Method Details
.included(other) ⇒ Object
:nodoc:
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/activefacts/api/entity.rb', line 431 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 |
# File 'lib/activefacts/api/entity.rb', line 162 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
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/activefacts/api/entity.rb', line 194 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 |
#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:
92 93 94 95 96 97 98 |
# File 'lib/activefacts/api/entity.rb', line 92 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
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 |