Module: Archimate::DataModel::Comparison

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



89
90
91
# File 'lib/archimate/data_model/comparison.rb', line 89

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/archimate/data_model/comparison.rb', line 37

def ==(other)
  return true if equal?(other)
  other.is_a?(self.class) &&
    self.class.comparison_attr_paths.all? do |attr|
      dig(*attr) == other.dig(*attr)
    end
end

#[](sym) ⇒ Object



45
46
47
# File 'lib/archimate/data_model/comparison.rb', line 45

def [](sym)
  send(sym)
end

#dig(*args) ⇒ Object



49
50
51
52
53
54
# File 'lib/archimate/data_model/comparison.rb', line 49

def dig(*args)
  return self if args.empty?
  val = send(args.shift)
  return val if args.empty?
  val&.dig(*args)
end

#each(&blk) ⇒ Object



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

def each(&blk)
  self.class.comparison_attr_paths.each(&blk)
end

#hashObject



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

def hash
  @hash_key ||=
    self.class.attr_names.reduce(self.class.hash) { |ha, attr| ha ^ send(attr).hash }
end

#initialize(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/archimate/data_model/comparison.rb', line 6

def initialize(opts = {})
  self.class.attr_info.each do |sym, attr_info|
    if attr_info.default == :value_required && !opts.include?(sym)
      raise "#{self.class} required value for #{sym} is missing."
    end
    val = opts.fetch(sym, attr_info.default)
    instance_variable_set("@#{sym}".to_sym, val)
    val.add_reference(self) if val.is_a?(Referenceable)
  end

  self.class.attr_info.each do |sym, attr_info|
    if attr_info.referenceable_list
      instance_variable_set(
        "@#{sym}".to_sym,
        ReferenceableList.new(self, opts.fetch(sym, attr_info.default), attr_info.also_reference)
      )
    elsif attr_info.also_reference.size.positive?
      val = instance_variable_get("@#{sym}".to_sym)
      attr_info.also_reference.each do |ref_sym|
        ref = send(ref_sym)
        val.add_reference(ref) if ref && val
      end
    end
  end
end

#inspectObject



81
82
83
84
85
86
87
# File 'lib/archimate/data_model/comparison.rb', line 81

def inspect
  "#<#{self.class.to_s.split('::').last}\n    " +
    self.class.attr_info
        .map { |sym, info| info.attr_inspect(self, sym) }
    .compact
        .join(" ") + ">"
end

#pretty_print(pp) ⇒ Object

TODO:

implement pretty_print as a more normal pretty_print with correct handling for attr_info values for comparison_attr



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/archimate/data_model/comparison.rb', line 66

def pretty_print(pp)
  pp.object_address_group(self) do
    pp.seplist(self.class.comparison_attr_paths, proc { pp.text ',' }) do |attr|
      column_value = dig(*attr)
      pp.breakable ' '
      pp.group(1) do
        pp.text Array(attr).map(&:to_s).join(".")
        pp.text ':'
        pp.breakable
        pp.pp column_value
      end
    end
  end
end

#to_hObject



56
57
58
# File 'lib/archimate/data_model/comparison.rb', line 56

def to_h
  self.class.attr_names.each_with_object({}) { |i, a| a[i] = send(i) }
end