Module: Archimate::DataModel::RelationshipReferences

Included in:
Element
Defined in:
lib/archimate/data_model/relationship_references.rb

Overview

RelationshipReferences provides a means to allow a class that is referenced by Relationship objects to get the set of:

  • All relationships

  • All relationships by:

    • a particular type

    • if this object is the source of target of the relationship

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_source_relationship_creation_method(rel_cls) ⇒ Object

Creates a method on this instance to create relationships to one or more elements of a the rel_cls relationship type where this object is source.

Parameters:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/archimate/data_model/relationship_references.rb', line 76

def self.define_source_relationship_creation_method(rel_cls)
  define_method(rel_cls::VERB.to_method_name) do |targets = nil, args = {}|
    rels = Array(args.fetch(:target, targets)).compact.map do |target|
      relationship = model&.relationships&.find do |r|
        r.is_a?(rel_cls) &&
          r.name == args.fetch(:name, nil) &&
          r.source == self &&
          r.target == target
      end
      unless relationship
        rargs = args.dup
        rargs[:target] = target
        rargs[:source] = self
        rargs[:id] = model.make_unique_id if !rargs.key?(:id) && model
        relationship = rel_cls.new(rargs)
        (model.relationships << relationship) if model
      end
      relationship
    end
    rels.size < 2 ? rels.first : rels
  end
end

.define_target_relationship_creation_method(rel_cls) ⇒ Object

Creates a method on this instance to create relationships to one or more elements of a the rel_cls relationship type where this object is target.

Parameters:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/archimate/data_model/relationship_references.rb', line 103

def self.define_target_relationship_creation_method(rel_cls)
  define_method(rel_cls::OBJECT_VERB.to_method_name) do |sources = nil, args = {}|
    rels = Array(args.fetch(:source, sources)).compact.map do |source|
      relationship = model&.relationships&.find do |r|
        r.is_a?(rel_cls) &&
          r.name == args.fetch(:name, nil) &&
          r.source == source &&
          r.target == self
      end
      unless relationship
        rargs = args.dup
        rargs[:source] = source
        rargs[:target] = self
        rargs[:id] = model.make_unique_id if !rargs.key?(:id) && model
        relationship = rel_cls.new(rargs)
        (model.relationships << relationship) if model
      end
      relationship
    end
    rels.size < 2 ? rels.first : rels
  end
end

.define_typed_elements_method(rel_cls) ⇒ Object

Creates a method on this instance that returns the elements related by the relationship type ‘rel_cls` where this object is the source.

Parameters:



42
43
44
45
46
47
48
# File 'lib/archimate/data_model/relationship_references.rb', line 42

def self.define_typed_elements_method(rel_cls)
  define_method(rel_cls::VERB.to_method_name("elements")) do
    references
      .select { |ref| ref.is_a?(rel_cls) && ref.source == self }
      .map(&:target)
  end
end

.define_typed_relationships_method(rel_cls) ⇒ Object

Creates a method on this instance that returns the relationships of the relationship type ‘rel_cls` where this object is the source.

Parameters:



32
33
34
35
36
# File 'lib/archimate/data_model/relationship_references.rb', line 32

def self.define_typed_relationships_method(rel_cls)
  define_method(rel_cls::VERB.to_method_name("relationships")) do
    references.select { |ref| ref.is_a?(rel_cls) && ref.source == self }
  end
end

.define_typed_targeted_elements_method(rel_cls) ⇒ Object

Creates a method on this instance that returns the elements related by the relationship type ‘rel_cls` where this object is the source.

Parameters:



64
65
66
67
68
69
70
# File 'lib/archimate/data_model/relationship_references.rb', line 64

def self.define_typed_targeted_elements_method(rel_cls)
  define_method(rel_cls::OBJECT_VERB.to_method_name("elements")) do
    references
      .select { |ref| ref.is_a?(rel_cls) && ref.target == self }
      .map(&:source)
  end
end

.define_typed_targeted_relationships_method(rel_cls) ⇒ Object

Creates a method on this instance that returns the relationships of the relationship type ‘rel_cls` where this object is the target.

Parameters:



54
55
56
57
58
# File 'lib/archimate/data_model/relationship_references.rb', line 54

def self.define_typed_targeted_relationships_method(rel_cls)
  define_method(rel_cls::OBJECT_VERB.to_method_name("relationships")) do
    references.select { |ref| ref.is_a?(rel_cls) && ref.target == self }
  end
end

.included(_base) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/archimate/data_model/relationship_references.rb', line 126

def self.included(_base)
  Relationships.classes.each do |rel_cls|
    define_typed_relationships_method(rel_cls)
    define_typed_elements_method(rel_cls)
    define_typed_targeted_relationships_method(rel_cls)
    define_typed_targeted_elements_method(rel_cls)
    define_source_relationship_creation_method(rel_cls)
    define_target_relationship_creation_method(rel_cls)
  end
end

Instance Method Details

#relationshipsObject



14
15
16
# File 'lib/archimate/data_model/relationship_references.rb', line 14

def relationships
  references.select { |ref| ref.is_a?(DataModel::Relationship) }
end

#source_relationshipsObject

List of relationships where this object is the source



19
20
21
# File 'lib/archimate/data_model/relationship_references.rb', line 19

def source_relationships
  relationships.select { |rel| rel.source.id == id }
end

#target_relationshipsObject

List of relationships where this object is the target



24
25
26
# File 'lib/archimate/data_model/relationship_references.rb', line 24

def target_relationships
  relationships.select { |rel| rel.target.id == id }
end