Method: Moura::Model::Diff#parse_hash_diff

Defined in:
lib/moura/model/diff.rb

#parse_hash_diff(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moura/model/diff.rb', line 22

def parse_hash_diff(data)
  data.each_with_object({}) do |(mark, target, diff), result|
    (role_name, child) = target.match(/^(.+?)(?:\.(apps|users)(?:\[\d+\])?)?$/).captures
    action = mark == "+" ? :add : :remove

    unless result[role_name]
      result[role_name] = {
        action: :none,
        apps: { add: [], remove: [] },
        users: { add: [], remove: [] }
      }
    end

    if child
      result[role_name][child.to_sym][action] <<= diff
    else
      result[role_name][:action] = action
      result[role_name][:apps][action] += diff.fetch("apps", [])
      result[role_name][:users][action] += diff.fetch("users", [])
    end
  end
end