Class: ActiveFacts::API::Role
Overview
A Role represents the relationship of one object to another (or to a boolean condition). Relationships (or binary fact types) have a Role at each end; one is declared using has_one or one_to_one, and the other is created on the counterpart class. Each ObjectType class maintains a RoleCollection hash of the roles it plays.
Instance Attribute Summary collapse
-
#counterpart ⇒ Object
All roles except unaries have a counterpart Role.
-
#is_identifying ⇒ Object
readonly
Is this an identifying role for object_type?.
-
#is_unary ⇒ Object
readonly
Returns the value of attribute is_unary.
-
#mandatory ⇒ Object
readonly
In a valid fact population, is this role required to be played?.
-
#name ⇒ Object
readonly
The name of the role (a Symbol).
-
#object_type ⇒ Object
readonly
The ObjectType to which this role belongs.
-
#unique ⇒ Object
readonly
Is this role played by at most one instance, or more?.
-
#value_constraint ⇒ Object
readonly
Counterpart Instances playing this role must meet this constraint.
Instance Method Summary collapse
-
#getter ⇒ Object
Return the name of the getter method.
-
#initialize(object_type, counterpart, name, mandatory = false, unique = true) ⇒ Role
constructor
A new instance of Role.
- #inspect ⇒ Object
-
#setter ⇒ Object
Return the name of the setter method.
-
#unary? ⇒ Boolean
Is this role a unary (created by maybe)? If so, it has no counterpart.
-
#variable ⇒ Object
Return the name of the instance variable.
- #verbalise ⇒ Object
Constructor Details
#initialize(object_type, counterpart, name, mandatory = false, unique = true) ⇒ Role
Returns a new instance of Role.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/activefacts/api/role.rb', line 26 def initialize(object_type, counterpart, name, mandatory = false, unique = true) @object_type = object_type @is_unary = counterpart == TrueClass @counterpart = @is_unary ? nil : counterpart @name = name @mandatory = mandatory @unique = unique @is_identifying = @object_type.is_entity_type && @object_type..include?(@name) associate_role(@object_type) end |
Instance Attribute Details
#counterpart ⇒ Object
All roles except unaries have a counterpart Role
20 21 22 |
# File 'lib/activefacts/api/role.rb', line 20 def counterpart @counterpart end |
#is_identifying ⇒ Object (readonly)
Is this an identifying role for object_type?
24 25 26 |
# File 'lib/activefacts/api/role.rb', line 24 def @is_identifying end |
#is_unary ⇒ Object (readonly)
Returns the value of attribute is_unary.
18 19 20 |
# File 'lib/activefacts/api/role.rb', line 18 def is_unary @is_unary end |
#mandatory ⇒ Object (readonly)
In a valid fact population, is this role required to be played?
22 23 24 |
# File 'lib/activefacts/api/role.rb', line 22 def mandatory @mandatory end |
#name ⇒ Object (readonly)
The name of the role (a Symbol)
19 20 21 |
# File 'lib/activefacts/api/role.rb', line 19 def name @name end |
#object_type ⇒ Object (readonly)
The ObjectType to which this role belongs
17 18 19 |
# File 'lib/activefacts/api/role.rb', line 17 def object_type @object_type end |
#unique ⇒ Object (readonly)
Is this role played by at most one instance, or more?
21 22 23 |
# File 'lib/activefacts/api/role.rb', line 21 def unique @unique end |
#value_constraint ⇒ Object (readonly)
Counterpart Instances playing this role must meet this constraint
23 24 25 |
# File 'lib/activefacts/api/role.rb', line 23 def value_constraint @value_constraint end |
Instance Method Details
#getter ⇒ Object
Return the name of the getter method
38 39 40 |
# File 'lib/activefacts/api/role.rb', line 38 def getter @getter ||= @name.to_sym end |
#inspect ⇒ Object
58 59 60 |
# File 'lib/activefacts/api/role.rb', line 58 def inspect "<Role #{object_type.name}.#{name}>" end |
#setter ⇒ Object
Return the name of the setter method
43 44 45 |
# File 'lib/activefacts/api/role.rb', line 43 def setter @setter ||= :"#{@name}=" end |
#unary? ⇒ Boolean
Is this role a unary (created by maybe)? If so, it has no counterpart
53 54 55 56 |
# File 'lib/activefacts/api/role.rb', line 53 def unary? # N.B. A role with a forward reference looks unary until it is resolved. counterpart == nil end |
#variable ⇒ Object
Return the name of the instance variable
48 49 50 |
# File 'lib/activefacts/api/role.rb', line 48 def variable @variable ||= "@#{@name}" end |
#verbalise ⇒ Object
62 63 64 65 |
# File 'lib/activefacts/api/role.rb', line 62 def verbalise "Role #{name} of #{object_type}, " + (@is_unary ? 'unary' : (counterpart ? 'played by' + counterpart.object_type : 'undefined')) end |