Class: Archimate::Diff::ArchimateNodeReference

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/diff/archimate_node_reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archimate_node) ⇒ ArchimateNodeReference

Returns a new instance of ArchimateNodeReference.



26
27
28
29
30
31
32
33
34
35
# File 'lib/archimate/diff/archimate_node_reference.rb', line 26

def initialize(archimate_node)
  unless archimate_node.is_a?(DataModel::ArchimateNode) || archimate_node.is_a?(Array)
    raise(
      TypeError,
      "archimate_node must be an ArchimateNode or Array, was #{archimate_node.class}"
    )
  end
  raise "new WTF? parent at path #{archimate_node.path} is a #{archimate_node.class} but isn't assigned a model" if archimate_node.in_model.nil? && !archimate_node.is_a?(DataModel::Model)
  @archimate_node = archimate_node
end

Instance Attribute Details

#archimate_nodeObject (readonly)

Returns the value of attribute archimate_node.



9
10
11
# File 'lib/archimate/diff/archimate_node_reference.rb', line 9

def archimate_node
  @archimate_node
end

Class Method Details

.for_node(node, child_node) ⇒ Object

There should be only a few things that are valid here:

  1. An archimate node and a attribute name sym

  2. An array and index

Produces a NodeReference instance for the given parameters



15
16
17
18
19
20
21
22
23
24
# File 'lib/archimate/diff/archimate_node_reference.rb', line 15

def self.for_node(node, child_node)
  case node
  when DataModel::ArchimateNode
    ArchimateNodeAttributeReference.new(node, child_node)
  when Array
    ArchimateArrayReference.new(node, child_node)
  else
    raise TypeError, "Node references need to be either an ArchimateNode or an Array"
  end
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
# File 'lib/archimate/diff/archimate_node_reference.rb', line 37

def ==(other)
  other.is_a?(self.class) &&
    value == other.value
end

#lookup_in_model(model) ⇒ Object



46
47
48
# File 'lib/archimate/diff/archimate_node_reference.rb', line 46

def lookup_in_model(model)
  recurse_lookup_in_model(archimate_node, model)
end

#lookup_parent_in_model(model) ⇒ Object



64
65
66
67
68
69
# File 'lib/archimate/diff/archimate_node_reference.rb', line 64

def lookup_parent_in_model(model)
  raise "WTF? parent at path #{path} is a #{parent.class} but isn't assigned a model" if parent.in_model.nil? && !parent.is_a?(DataModel::Model)
  result = recurse_lookup_in_model(parent, model)
  $stderr.puts "Unable to lookup parent with path #{path}" if result.nil?
  result
end

#parentObject



71
72
73
# File 'lib/archimate/diff/archimate_node_reference.rb', line 71

def parent
  archimate_node
end

#path(options = {}) ⇒ Object



75
76
77
# File 'lib/archimate/diff/archimate_node_reference.rb', line 75

def path(options = {})
  @node_ref_path ||= archimate_node.path(options)
end

#recurse_lookup_in_model(node, model) ⇒ Object

Raises:

  • (TypeError)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/archimate/diff/archimate_node_reference.rb', line 50

def recurse_lookup_in_model(node, model)
  return nil if node.nil?
  raise TypeError, "node argument must be ArchimateNode or Array, was a #{node.class}" unless node.is_a?(Array) || node.is_a?(DataModel::ArchimateNode)
  raise TypeError, "model argument must be a Model, was a #{model.class}" unless model.is_a?(DataModel::Model)
  if node.is_a?(DataModel::Model)
    return model
  elsif node.is_a?(DataModel::Referenceable)
    return model.lookup(node.id)
  else
    node_parent_in_model = recurse_lookup_in_model(node.parent, model)
    node_parent_in_model[node.parent_attribute_name] unless node_parent_in_model.nil?
  end
end

#to_sObject



42
43
44
# File 'lib/archimate/diff/archimate_node_reference.rb', line 42

def to_s
  value.to_s
end