Module: HappyMapper::DiffedItem

Defined in:
lib/happymapper/differ.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comparedObject Also known as: was

The object this item is being compared to



89
90
91
# File 'lib/happymapper/differ.rb', line 89

def compared
  @compared
end

Class Method Details

.create(item, compared) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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?Boolean

Returns:

  • (Boolean)


92
93
94
# 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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
# File 'lib/happymapper/differ.rb', line 116

def changed?
  if self.is_a?(HappyMapper)
    ! changes.empty?
  else
    self != compared
  end
end

#changesObject



124
125
126
# File 'lib/happymapper/differ.rb', line 124

def changes
  @changes ||= ChangeLister.new(self, compared).find_changes
end