Class: HappyMapper::ChangeLister
- Inherits:
-
Object
- Object
- HappyMapper::ChangeLister
- Defined in:
- lib/happymapper/differ.rb
Overview
ChangeLister creates a hash of all changes between the two objects
Instance Method Summary collapse
- #elements_and_attributes ⇒ Object
- #eq(a, b) ⇒ Object
- #find_changes ⇒ Object
- #get_compared_value(key) ⇒ Object
-
#initialize(current, compared) ⇒ ChangeLister
constructor
A new instance of ChangeLister.
-
#many_changes(els, key:) ⇒ Object
Handle change for has_many elements.
Constructor Details
#initialize(current, compared) ⇒ ChangeLister
Returns a new instance of ChangeLister.
111 112 113 114 115 |
# File 'lib/happymapper/differ.rb', line 111 def initialize(current, compared) @current = current @compared = compared @changes = {} end |
Instance Method Details
#elements_and_attributes ⇒ Object
162 163 164 |
# File 'lib/happymapper/differ.rb', line 162 def elements_and_attributes @current.class.attributes + @current.class.elements end |
#eq(a, b) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/happymapper/differ.rb', line 117 def eq(a,b) if a.respond_to?(:to_xml) && b.respond_to?(:to_xml) a.to_xml == b.to_xml else a == b end end |
#find_changes ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/happymapper/differ.rb', line 125 def find_changes elements_and_attributes.map(&:name).each do |name| el = @current.send(name) if el.is_a?(Array) many_changes(el, key: name) else other_el = get_compared_value(name) if ! eq(el, other_el) @changes[name] = other_el end end end @changes end |
#get_compared_value(key) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/happymapper/differ.rb', line 154 def get_compared_value(key) if @compared.respond_to?(key) @compared.send(key) else nil end end |
#many_changes(els, key:) ⇒ Object
Handle change for has_many elements
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/happymapper/differ.rb', line 143 def many_changes(els, key:) other_els = get_compared_value(key) || [] els.each_with_index do |el, i| if ! eq(el, other_els[i]) @changes[key] ||= [] @changes[key] << other_els[i] end end end |