Module: ActiveFacts::API::Entity
- Includes:
- Instance
- Defined in:
- lib/activefacts/api/entity.rb,
lib/activefacts/persistence/concept.rb
Overview
An Entity type is any Concept 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
-
#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 ⇒ Object
Return the array of the values of this entity instance’s identifying roles.
-
#initialize(*args) ⇒ Object
Assign the identifying roles to initialise a new Entity instance.
-
#inspect ⇒ Object
:nodoc:.
-
#verbalise(role_name = nil) ⇒ Object
Verbalise this entity instance.
Methods included from Instance
Class Method Details
.included(other) ⇒ Object
:nodoc:
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/activefacts/api/entity.rb', line 243 def Entity.included other #:nodoc: other.send :extend, ClassMethods # Register ourselves with the parent module, which has become a Vocabulary: vocabulary = other.modspace # puts "Entity.included(#{other.inspect})" unless vocabulary.respond_to? :concept # Extend module with Vocabulary if necessary vocabulary.send :extend, Vocabulary end vocabulary.__add_concept(other) end |
Instance Method Details
#eql?(other) ⇒ Boolean
When used as a hash key, this entity instance is compared with another by comparing the values of its identifying roles
77 78 79 80 81 82 83 |
# File 'lib/activefacts/api/entity.rb', line 77 def eql?(other) return false unless self.class == other.class self.class..each{|role| return false unless send(role).eql?(other.send(role)) } return true 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
66 67 68 69 70 71 72 73 |
# File 'lib/activefacts/api/entity.rb', line 66 def hash self.class..map{|role| instance_variable_get("@#{role}") }.inject(0) { |h,v| h ^= v.hash h } end |
#identifying_role_values ⇒ Object
Return the array of the values of this entity instance’s identifying roles
97 98 99 100 101 |
# File 'lib/activefacts/api/entity.rb', line 97 def self.class..map{|role| send(role) } end |
#initialize(*args) ⇒ Object
Assign the identifying roles to initialise a new Entity instance. The role values are asserted in the constellation first, so you can pass bare values (array, string, integer, etc) for any role whose instances can be constructed using those values.
A value must be provided for every identifying role, but if the last argument is a hash, they may come from there.
Any additional (non-identifying) roles may also be passed in the final hash.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/activefacts/api/entity.rb', line 23 def initialize(*args) super(args) klass = self.class hash = {} hash = args.pop.clone if Hash === args[-1] # Pick any missing identifying roles out of the hash if possible: while args.size < (ir = klass.).size value = hash[role = ir[args.size]] hash.delete(role) args.push value end # If one arg is expected but more are passed, they might be the args for the object that plays the identifying role: args = [args] if klass..size == 1 && args.size > 1 # This should now only occur when there are too many args passed: raise "Wrong number of parameters to #{klass}.new, " + "expect (#{klass.*","}) " + "got (#{args.map{|a| a.to_s.inspect}*", "})" if args.size != klass..size # Assign the identifying roles in order, then the other roles passed as a hash: (klass..zip(args) + hash.entries).each do |role_name, value| role = klass.roles(role_name) send("#{role_name}=", value) end end |
#inspect ⇒ Object
:nodoc:
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/activefacts/api/entity.rb', line 51 def inspect #:nodoc: "\#<#{ self.class.basename }:#{ object_id }#{ constellation ? " in #{constellation.inspect}" : "" } #{ # REVISIT: Where there are one-to-one roles, this cycles self.class..map{|role| "@#{role}="+send(role).inspect }*" " }>" end |
#verbalise(role_name = nil) ⇒ Object
Verbalise this entity instance
86 87 88 89 90 91 92 93 94 |
# File 'lib/activefacts/api/entity.rb', line 86 def verbalise(role_name = nil) "#{role_name || self.class.basename}(#{ self.class..map{|role_sym| value = send(role_sym) role_name = self.class.roles(role_sym).name.to_s.camelcase(true) value ? value.verbalise(role_name) : "nil" }*", " })" end |