module Riffer::Rig::Support::JsonFile
Public Instance Methods
(String path, ?symbolize_names: bool) → (Hash[untyped, untyped] | String)
Source
# File lib/riffer/rig/support/json_file.rb, line 12 def read(path, symbolize_names: false) content = File.file?(path) ? File.read(path) : '' empty = {} #: Hash[untyped, untyped] return empty if content.b.strip.empty? document = JSON.parse(content, symbolize_names: symbolize_names) document.is_a?(Hash) ? document : "#{path} does not hold a JSON object" rescue JSON::ParserError => e # The parser's message quotes the text it stopped at, which in auth.json # can be a secret, so only the position is taken from it. ["#{path} is not valid JSON", e.message[/line \d+ column \d+\z/]].compact.join(' at ') end
@rbs path: String @rbs symbolize_names: bool @rbs return: (Hash[untyped, untyped] | String)
(String path, ?symbolize_names: bool, ?mode: Integer?) { (Hash[untyped, untyped]) → Hash[untyped, untyped] } → String?
Source
# File lib/riffer/rig/support/json_file.rb, line 30 def update(path, symbolize_names: false, mode: nil) document = read(path, symbolize_names: symbolize_names) return document if document.is_a?(String) updated = yield document write(path, updated, mode: mode) unless updated == document nil end
@rbs path: String @rbs symbolize_names: bool @rbs mode: Integer? @rbs &: (Hash[untyped, untyped]) -> Hash[untyped, untyped] @rbs return: String?