Module: LunaPark::Extensions::ComparableDebug

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

Overview

Debug for #== method

Examples:

class Funds
  include LunaPark::Extensions::Comparable
  include LunaPark::Extensions::ComparableDebug
  # ...

  comparable_attributes :from, :to
  # ...
end

Instance Method Summary collapse

Instance Method Details

#detailed_comparsion(other) ⇒ Object Also known as: detailed_cmp

Returns nested Hash represents full comparsion that contains:

In root:
{ `bool` => { ... } } (that describes result of compasrion with detailed cmp in hash value)
AND
  { :field_name => { `bool` => ... } } (that describes concrete field comparsion)
  OR
  { `bool` => [left, right] } } (that describes result of left and right objects compasrion)

Examples:

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

t1 == t2 # => false

t1.detailed_cmp(t2) # =>
  { false => {                                         # (t1 == t2) == false
      :from => {                                       #   `#from`
        false => {                                     #     (t1.from == t2.from) == false
          :account =>                                  #   `#from#account`
            { true => [nil, nil] },                    #     (t1.from.account == t2.from.account) == (nil == nil)
          :charge => {                                 #   `#from#charge`
            false => {                                 #     (t1.from.charge == t2.from.charge) == false
              :currency => { true => ["USD", "USD"] }, #       (t1.from.charge.currency == t2.from.charge.currency) == ('USD' == 'USD')
              :amount => { false => [42, 43] } } },    #       (t1.from.amount == t2.from.amount) == (42 == 43)
          :usd => {                                    #   `#from#usd`
            false => {                                 #     (t1.from.usd == t2.from.usd) == false
              :currency => { true => ["USD", "USD"] }, #       (t1.from.usd.currency == t2.from.usd.currency) == ('USD' == 'USD')
              :amount => { false => [41, 44] } } } }   #       (t1.from.usd.amount == t2.from.usd.amount) == (41 == 44)
      }
    }
  }


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/luna_park/extensions/comparable_debug.rb', line 50

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

    output[field] = if left.respond_to?(:detailed_comparsion)
                      left.detailed_comparsion(right)
                    else
                      { (left == right) => [left, right] }
                    end
  end

  { (self == other) => diff }
end

#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] } }


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/luna_park/extensions/comparable_debug.rb', line 78

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