class TyranoDsl::ExportGame::FileActions::JsonPatch

Path a JSON file

Attributes

file_path[R]

@return [String]

patched_content[R]

@return [Object]

patching_path[R]

@return [Array<String>]

Public Class Methods

new(file_path, patching_path, patched_content) click to toggle source

@param [String] file_path @param [Array<String>] patching_path @param [Object] patched_content

# File lib/tyrano_dsl/export_game/file_actions/json_patch.rb, line 20
def initialize(file_path, patching_path, patched_content)
  @file_path = file_path
  @patching_path = patching_path
  @patched_content = patched_content
  log {self.to_s}
end

Public Instance Methods

run(tyrano_project_path) click to toggle source

@param [String] tyrano_project_path @return [void]

# File lib/tyrano_dsl/export_game/file_actions/json_patch.rb, line 29
def run(tyrano_project_path)
  full_path = File.join(tyrano_project_path, file_path)
  log {"Patching file [#{full_path}] at #{patching_path}"}
  unless File.exist? full_path
    raise TyranoDsl::TyranoException, "Missing file [#{full_path}]"
  end
  content = JSON.parse(IO.read(full_path))

  # Going down on the tree …
  current_subtree = content
  0.upto(patching_path.length - 2) do |path_segment_index|
    current_subtree = current_subtree[patching_path[path_segment_index]]
  end
  # … and patch the leaf
  current_subtree[patching_path.last] = patched_content

  File.write(full_path, JSON.pretty_generate(content))
end
to_s() click to toggle source
# File lib/tyrano_dsl/export_game/file_actions/json_patch.rb, line 48
def to_s
  "Patch [#{file_path}] at #{patching_path}"
end