Class: ActiveFacts::API::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/api/role.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(fact_type, object_type, role_name, mandatory, unique, restrict = nil) ⇒ Role

Returns a new instance of Role.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/activefacts/api/role.rb', line 29

def initialize(fact_type, object_type, role_name, mandatory, unique, restrict = nil)
  @fact_type = fact_type
  @fact_type.all_role << self
  @object_type = object_type
  @name = role_name
  @mandatory = mandatory
  @unique = unique
  @value_constraint = restrict
  object_type.add_role(self)
  associate_role(@object_type)
end

Instance Attribute Details

#fact_typeObject (readonly)

The FactType to which this role belongs



17
18
19
# File 'lib/activefacts/api/role.rb', line 17

def fact_type
  @fact_type
end

#mandatoryObject (readonly)

In a valid fact population, is this role required to be played?



21
22
23
# File 'lib/activefacts/api/role.rb', line 21

def mandatory
  @mandatory
end

#nameObject (readonly)

The name of the role (a Symbol)



19
20
21
# File 'lib/activefacts/api/role.rb', line 19

def name
  @name
end

#object_typeObject (readonly)

The ObjectType to which this role belongs



18
19
20
# File 'lib/activefacts/api/role.rb', line 18

def object_type
  @object_type
end

#uniqueObject (readonly)

Is this role played by at most one instance, or more?



20
21
22
# File 'lib/activefacts/api/role.rb', line 20

def unique
  @unique
end

#value_constraintObject (readonly)

Counterpart Instances playing this role must meet this constraint



22
23
24
# File 'lib/activefacts/api/role.rb', line 22

def value_constraint
  @value_constraint
end

Instance Method Details

#counterpartObject



52
53
54
# File 'lib/activefacts/api/role.rb', line 52

def counterpart
  @counterpart ||= (@fact_type.all_role - [self])[0]
end

#getterObject

Return the name of the getter method



57
58
59
# File 'lib/activefacts/api/role.rb', line 57

def getter
  @getter ||= @name.to_sym
end

#inspectObject



71
72
73
# File 'lib/activefacts/api/role.rb', line 71

def inspect
  "<Role #{object_type.name}.#{name}>"
end

#is_identifyingObject

Is this an identifying role for object_type?



24
25
26
27
# File 'lib/activefacts/api/role.rb', line 24

def is_identifying # Is this an identifying role for object_type?
  return @is_identifying unless @is_identifying == nil
  @is_identifying = !!(@object_type.is_entity_type && @object_type.identifying_role_names.include?(@name))
end

#make_mandatoryObject



47
48
49
50
# File 'lib/activefacts/api/role.rb', line 47

def make_mandatory
  # Sometimes a role has already been defined from the other end
  @mandatory = true
end

#setterObject

Return the name of the setter method



62
63
64
# File 'lib/activefacts/api/role.rb', line 62

def setter
  @setter ||= :"#{@name}="
end

#unary?Boolean

Is this role a unary (created by maybe)?

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/activefacts/api/role.rb', line 42

def unary?
  # N.B. A role with a forward reference looks unary until it is resolved.
  @fact_type.all_role.size == 1
end

#variableObject

Return the name of the instance variable



67
68
69
# File 'lib/activefacts/api/role.rb', line 67

def variable
  @variable ||= "@#{@name}"
end

#verbaliseObject



75
76
77
78
# File 'lib/activefacts/api/role.rb', line 75

def verbalise
  "Role #{name} of #{object_type}, " +
    (unary? ? 'unary' : (counterpart ? 'played by' + counterpart.object_type : 'undefined'))
end