Class: D2E
- Inherits:
-
Object
- Object
- D2E
- Defined in:
- lib/d2e.rb,
lib/d2e/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #diff(prev, curr) ⇒ Object
-
#initialize(options) ⇒ D2E
constructor
A new instance of D2E.
Constructor Details
#initialize(options) ⇒ D2E
Returns a new instance of D2E.
4 5 6 |
# File 'lib/d2e.rb', line 4 def initialize() @key = [:key] end |
Instance Method Details
#diff(prev, curr) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/d2e.rb', line 8 def diff(prev, curr) events = [] prev_ids = prev.map {|item| item[@key] } curr_ids = curr.map {|item| item[@key] } deleted = prev_ids - curr_ids created = curr_ids - prev_ids updated = prev_ids & curr_ids created.each do |id| item = curr.find {|item| item[@key] == id } events << {'type' => 'create', 'item' => item} end deleted.each do |id| item = prev.find {|item| item[@key] == id } events << {'type' => 'delete', 'item' => item} end updated.each do |id| diff = {} prev_item = prev.find {|item| item[@key] == id } curr_item = curr.find {|item| item[@key] == id } keys = prev_item.keys | curr_item.keys keys.each do |key| prev_value = prev_item[key] curr_value = curr_item[key] if prev_value != curr_value diff[key] = [prev_value, curr_value] end end if !diff.empty? events << {'type' => 'update', 'id' => id, 'diff' => diff} end end events end |