Class: ActiveShepherd::DeepReverseChanges

Inherits:
Struct
  • Object
show all
Defined in:
lib/active_shepherd/deep_reverse_changes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#changesObject

Returns the value of attribute changes

Returns:

  • (Object)

    the current value of changes



1
2
3
# File 'lib/active_shepherd/deep_reverse_changes.rb', line 1

def changes
  @changes
end

Instance Method Details

#reverseObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_shepherd/deep_reverse_changes.rb', line 2

def reverse
  changer = ->(h) {
    unless h.is_a?(Hash)
      binding.pry
    end
    h.each_with_object({}) do |(k,v), new_hash|
      if v.is_a?(Array) && v.size == 2
        new_hash[k] = [v.last, v.first]
      elsif v.is_a?(Hash)
        new_hash[k] = changer.call(v)
      elsif :_create == k.to_sym
        new_hash[:_destroy] = v
      elsif :_destroy == k.to_sym
        new_hash[:_create] = v
      end
    end
  }

  changer.call(changes)
end