Class: Archimate::DataModel::ArchimateNode

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/archimate/data_model/archimate_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ArchimateNode

Returns a new instance of ArchimateNode.



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

def initialize(attributes)
  super
  @struct_instance_variables = self.class.schema.keys
end

Instance Attribute Details

#parent_attribute_nameObject



82
83
84
85
# File 'lib/archimate/data_model/archimate_node.rb', line 82

def parent_attribute_name
  return @parent_attribute_name if defined?(@parent_attribute_name)
  parent.find_index(self) if parent&.is_a?(Array)
end

#struct_instance_variablesObject (readonly)

Returns the value of attribute struct_instance_variables.



11
12
13
# File 'lib/archimate/data_model/archimate_node.rb', line 11

def struct_instance_variables
  @struct_instance_variables
end

Instance Method Details

#ancestorsObject



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

def ancestors
  result = [self]
  p = self
  result << p until (p = p.parent).nil?
  result
end

#build_index(hash_index = {}) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/archimate/data_model/archimate_node.rb', line 94

def build_index(hash_index = {})
  hash_index[id] = self unless id.nil?
  struct_instance_variables.reduce(hash_index) do |a, e|
    self[e].parent_attribute_name = e
    self[e].build_index(a)
  end
end

#cloneObject

Note: my clone method does one non-idiomatic thing - it does not clone the frozen state. TODO: respect the frozen state of the clone’d object.



31
32
33
34
35
36
37
38
# File 'lib/archimate/data_model/archimate_node.rb', line 31

def clone
  self.class.new(
    struct_instance_variables
      .each_with_object({}) do |i, a|
        a[i] = self[i].primitive? ? self[i] : self[i].clone
      end
  )
end

#compact!Object



124
125
126
127
# File 'lib/archimate/data_model/archimate_node.rb', line 124

def compact!
  struct_instance_variables.each { |attrname| self[attrname].compact! }
  self
end

#delete(attrname) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/archimate/data_model/archimate_node.rb', line 129

def delete(attrname)
  if !attrname || attrname.empty?
    raise(
      ArgumentError,
      "attrname was blank must be one of: #{struct_instance_variables.map(&:to_s).join(',')}"
    )
  end
  in_model&.deregister(self[attrname])
  instance_variable_set("@#{attrname}".to_sym, nil)
  self
end

#diff(other) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/archimate/data_model/archimate_node.rb', line 102

def diff(other)
  raise ArgumentError, "other expected to be not nil" if other.nil?
  raise TypeError, "Expected other <#{other.class} to be of type #{self.class}" unless other.is_a?(self.class)
  struct_instance_variables.each_with_object([]) do |k, a|
    val = self[k]
    if val.nil?
      a.concat([Diff::Insert.new(Diff::ArchimateNodeAttributeReference.new(other, k))]) unless other[k].nil?
    elsif val.primitive?
      a.concat(val.diff(other[k], self, other, k))
    else
      a.concat(val.diff(other[k]))
    end
  end
end

#dupObject

Makes a copy of the archimate node which is not frozen



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

def dup
  self.class.new(
    struct_instance_variables
      .each_with_object({}) do |i, a|
        a[i] = self[i].primitive? ? self[i] : self[i].dup
      end
  )
end

#element_by_id(element_id) ⇒ Object



160
161
162
163
# File 'lib/archimate/data_model/archimate_node.rb', line 160

def element_by_id(element_id)
  return nil unless element_id
  in_model&.lookup(element_id)
end

#idObject

TODO: this is used only such that every item has an id for sticking in the index. Is this really needed still?



60
61
62
# File 'lib/archimate/data_model/archimate_node.rb', line 60

def id
  object_id
end

#in_modelObject



50
51
52
# File 'lib/archimate/data_model/archimate_node.rb', line 50

def in_model
  @in_model if defined?(@in_model)
end

#in_model=(model) ⇒ Object



87
88
89
90
91
92
# File 'lib/archimate/data_model/archimate_node.rb', line 87

def in_model=(model)
  @in_model = model unless is_a?(Model)
  struct_instance_variables.each { |attrname|
    puts "#{attrname} is frozen in #{self.class}" if self[attrname].frozen? && self[attrname].is_a?(Array)
    self[attrname].in_model = model }
end

#parentObject



54
55
56
# File 'lib/archimate/data_model/archimate_node.rb', line 54

def parent
  @parent if defined?(@parent)
end

#parent=(par) ⇒ Object



75
76
77
78
79
80
# File 'lib/archimate/data_model/archimate_node.rb', line 75

def parent=(par)
  @parent = par
  struct_instance_variables.each do |attrname|
    self[attrname].parent = self
  end
end

#path(options = {}) ⇒ Object



117
118
119
120
121
122
# File 'lib/archimate/data_model/archimate_node.rb', line 117

def path(options = {})
  [
    parent&.path(options),
    path_identifier
  ].compact.map(&:to_s).reject(&:empty?).join("/")
end

#primitive?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/archimate/data_model/archimate_node.rb', line 71

def primitive?
  false
end

#referenced_identified_nodesObject



154
155
156
157
158
# File 'lib/archimate/data_model/archimate_node.rb', line 154

def referenced_identified_nodes
  struct_instance_variables.reduce([]) do |a, e|
    a.concat(self[e].referenced_identified_nodes)
  end
end

#set(attrname, value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/archimate/data_model/archimate_node.rb', line 141

def set(attrname, value)
  if !attrname
    raise(
      ArgumentError,
      "attrname was blank must be one of: #{struct_instance_variables.map(&:to_s).join(',')}"
    )
  end #  || attrname.empty?
  # value = value.clone
  in_model&.register(value, self)
  instance_variable_set("@#{attrname}".to_sym, value)
  self
end

#with(options = {}) ⇒ Object

TODO: Schedule this for eliminations Outside of test code, this is only used for the Bounds class



20
21
22
23
24
25
26
27
# File 'lib/archimate/data_model/archimate_node.rb', line 20

def with(options = {})
  self.class.new(
    struct_instance_variables
      .each_with_object({}) { |i, a| a[i] = self[i] }
      .merge(options)
      .each_with_object({}) { |(k, v), a| a[k] = v.dup }
  )
end