Module: LunaPark::Extensions::Comparable::InstanceMethods

Defined in:
lib/luna_park/extensions/comparable.rb

Instance Method Summary collapse

Instance Method Details

#detailed_differences(other) ⇒ Object Also known as: detailed_diff

Returns only different values, that causes missmatch

Examples:

t1 = Funds.new(from: { charge: { currency: 'USD', amount: 42 } }, usd: { currency: 'USD', amount: 41 }, comment: 'Foo')
t2 = Funds.new(from: { charge: { currency: 'USD', amount: 43 } }, usd: { currency: 'USD', amount: 42 }, comment: 'Foo')

t1 == t2 # => false

t1.detailed_diff(t2) # =>
 { from: { charge: { amount: [42, 43] } }, usd: { amount: [41, 42] } }


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/luna_park/extensions/comparable.rb', line 90

def detailed_differences(other)
  self.class.comparable_attributes_list.each_with_object({}) do |field, output|
    left  = send(field)
    right = other&.send(field)

    next if left == right

    output[field] = if left.respond_to?(:detailed_differences)
                      left.detailed_differences(right)
                    else
                      [left, right]
                    end
  end
end

#enable_debugObject Also known as: debug

Enable debug mode (just include debug methods)



109
110
111
112
# File 'lib/luna_park/extensions/comparable.rb', line 109

def enable_debug
  self.class.enable_debug
  self
end

#eql?(other) ⇒ Boolean Also known as: ==

Compare this object with other using methids, described with ‘::comparable_attributes` method

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/luna_park/extensions/comparable.rb', line 71

def eql?(other)
  return false unless other.is_a?(self.class)

  self.class.comparable_attributes_list.all? { |attr| send(attr) == other.send(attr) }
end