module StateMate::Adapters::JSON
Public Class Methods
parse_key(key)
click to toggle source
# File lib/state_mate/adapters/json.rb, line 10 def self.parse_key key # use the same key separation as Defaults StateMate::Adapters::Defaults.parse_key key end
read(key, options = {})
click to toggle source
# File lib/state_mate/adapters/json.rb, line 15 def self.read key, options = {} filepath, key_segs = parse_key key contents = File.read(File.expand_path(filepath)) value = ::JSON.load contents key_segs.each do |seg| value = if (value.is_a?(Hash) && value.key?(seg)) value[seg] else nil end end value end
write(key, value, options = {})
click to toggle source
# File lib/state_mate/adapters/json.rb, line 33 def self.write key, value, options = {} options = { 'pretty' => true, }.merge options filepath, key_segs = parse_key key StateMate.debug "writing json", options: options, filepath: filepath, key_segs: key_segs new_root = if key_segs.empty? value else root = read filepath StateMate::Adapters::Defaults.hash_deep_write!( root, key_segs, value ) root end StateMate.debug new_root: new_root content = if options['pretty'] ::JSON.pretty_generate new_root else ::JSON.dump new_root end File.open(filepath, 'w') do |f| f.write content end end