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.
131 132 133 134 135 |
# File 'lib/happymapper/differ.rb', line 131 def initialize(current, compared) @current = current @compared = compared @changes = {} end |
Instance Method Details
#elements_and_attributes ⇒ Object
182 183 184 |
# File 'lib/happymapper/differ.rb', line 182 def elements_and_attributes @current.class.attributes + @current.class.elements end |
#eq(a, b) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/happymapper/differ.rb', line 137 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/happymapper/differ.rb', line 145 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
174 175 176 177 178 179 180 |
# File 'lib/happymapper/differ.rb', line 174 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
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/happymapper/differ.rb', line 163 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 |