Module: Archimate::DataModel::Differentiable

Defined in:
lib/archimate/data_model/differentiable.rb

Instance Method Summary collapse

Instance Method Details

#diff(other) ⇒ Array<Diff::Difference>

Computes the diffs between this object and another object of the same type

Parameters:

  • other (self.class)

    another object to compare

Returns:

  • (Array<Diff::Difference>)

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/archimate/data_model/differentiable.rb', line 90

def diff(other)
  return [] if other.nil?
  raise TypeError, "Expected other <#{other.class} to be of type #{self.class}" unless other.is_a?(self.class)

  self.class.attr_names.each_with_object([]) do |k, a|
    val = send(k)
    case val
    when NilClass
      a << Insert.new(k, other[k]) unless other[k].nil?
    when Integer, Float, Hash, String, Symbol
      a.concat(Differentiable.diff_primitive(val, other[k], self, other, k))
    when Differentiable
      a.concat(val.diff(other[k]))
    else
      raise "Unexpected Type for Diff don't know how to diff a #{val.class}"
    end
  end
end

#patch(diffs) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/archimate/data_model/differentiable.rb', line 109

def patch(diffs)
  ary = diffs.is_a?(Array) ? diffs : [diffs]
  self.class.new(
    ary.each_with_object(to_h) do |diff, args|
      case diff
      when Delete
        args[diff.path] = nil
      when Insert
        args[diff.path] = diff.value
      when Change
        args[diff.path] = diff.to
      else
        raise "Unexpected diff type #{diff.class} #{diff.inspect}"
      end
    end
  )
end