Class: TyranoDsl::ExportGame::FileActions::JsonPatch

Inherits:
Object
  • Object
show all
Includes:
FileActionsModule
Defined in:
lib/tyrano_dsl/export_game/file_actions/json_patch.rb

Overview

Path a JSON file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, patching_path, patched_content) ⇒ JsonPatch

Returns a new instance of JsonPatch.

Parameters:

  • file_path (String)
  • patching_path (Array<String>)
  • patched_content (Object)


20
21
22
23
24
25
# 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

Instance Attribute Details

#file_pathString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 11

def file_path
  @file_path
end

#patched_contentObject (readonly)

Returns:

  • (Object)


13
14
15
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 13

def patched_content
  @patched_content
end

#patching_pathArray<String> (readonly)

Returns:

  • (Array<String>)


15
16
17
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 15

def patching_path
  @patching_path
end

Instance Method Details

#run(tyrano_project_path) ⇒ void

This method returns an undefined value.

Parameters:

  • tyrano_project_path (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_sObject



48
49
50
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 48

def to_s
  "Patch [#{file_path}] at #{patching_path}"
end