module HappyMapper::DiffedItem

DiffedItem is an extension which allows tracking changes between two HappyMapper objects.

Attributes

compared[RW]

The object this item is being compared to

was[RW]

The object this item is being compared to

Public Class Methods

create(item, compared) click to toggle source
# File lib/happymapper/differ.rb, line 96
def self.create(item, compared)
  begin
    # if the item can not be cloned, it will raise an exception
    # do not extend objects which can not be cloned
    if ruby_2_3_or_older?
      item.clone
    else
      item.clone(freeze: false)
    end
    item.extend(DiffedItem)
  rescue
    # this item is a Float, Nil or other class that can not be extended
    item = HappyMapper::UnExtendable.new(item)
    item.extend(DiffedItem)
  end

  item.compared = compared
  item
end
ruby_2_3_or_older?() click to toggle source
# File lib/happymapper/differ.rb, line 92
def self.ruby_2_3_or_older?
  @ruby_2_3_or_older ||= Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4')
end

Public Instance Methods

changed?() click to toggle source
# File lib/happymapper/differ.rb, line 116
def changed?
  if self.is_a?(HappyMapper)
    ! changes.empty?
  else
    self != compared
  end
end
changes() click to toggle source
# File lib/happymapper/differ.rb, line 124
def changes
  @changes ||= ChangeLister.new(self, compared).find_changes
end