class Bulldozer::Actions::ExpandJson

Attributes

data[R]
destination_root[R]
file[R]

Public Class Methods

new(destination_root, file, data) click to toggle source
# File lib/bulldozer/actions.rb, line 35
def initialize(destination_root, file, data)
  @destination_root = destination_root
  @file = file
  @data = data
end

Public Instance Methods

invoke!() click to toggle source
# File lib/bulldozer/actions.rb, line 41
def invoke!
  write_out { |existing_json| existing_json.merge(data) }
end
revoke!() click to toggle source
# File lib/bulldozer/actions.rb, line 45
def revoke!
  write_out { |existing_json| hash_unmerge(existing_json, data) }
end

Private Instance Methods

destination_file() click to toggle source
# File lib/bulldozer/actions.rb, line 58
def destination_file
  File.join(destination_root, file)
end
existing_json() click to toggle source
# File lib/bulldozer/actions.rb, line 62
def existing_json
  JSON.parse(IO.read(destination_file))
rescue Errno::ENOENT
  {}
end
hash_unmerge(hash, subhash) click to toggle source
# File lib/bulldozer/actions.rb, line 68
def hash_unmerge(hash, subhash)
  subhash.reduce(hash) do |acc, (k, v)|
    if hash.has_key?(k)
      if v == hash[k]
        acc.except(k)
      elsif v.is_a?(Hash)
        acc.merge(k => hash_unmerge(hash[k], v))
      else
        acc
      end
    else
      acc
    end
  end
end
write_out() { |existing_json| ... } click to toggle source
# File lib/bulldozer/actions.rb, line 53
def write_out
  new_json = yield(existing_json)
  IO.write(destination_file, JSON.pretty_generate(new_json))
end