Class: TyranoDsl::ExportGame::FileActions::JsonPatch
- Inherits:
-
Object
- Object
- TyranoDsl::ExportGame::FileActions::JsonPatch
- Includes:
- FileActionsModule
- Defined in:
- lib/tyrano_dsl/export_game/file_actions/json_patch.rb
Overview
Path a JSON file
Instance Attribute Summary collapse
- #file_path ⇒ String readonly
- #patched_content ⇒ Object readonly
- #patching_path ⇒ Array<String> readonly
Instance Method Summary collapse
-
#initialize(file_path, patching_path, patched_content) ⇒ JsonPatch
constructor
A new instance of JsonPatch.
- #run(tyrano_project_path) ⇒ void
- #to_s ⇒ Object
Constructor Details
#initialize(file_path, patching_path, patched_content) ⇒ JsonPatch
Returns a new instance of JsonPatch.
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_path ⇒ String (readonly)
11 12 13 |
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 11 def file_path @file_path end |
#patched_content ⇒ Object (readonly)
13 14 15 |
# File 'lib/tyrano_dsl/export_game/file_actions/json_patch.rb', line 13 def patched_content @patched_content end |
#patching_path ⇒ Array<String> (readonly)
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.
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_s ⇒ Object
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 |