Sha256: 7dbccd186bad1c588ff9b49f26572c8af06665bd9081a797bd3cb5cada5bf3c9

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'json'

require 'state_mate'
require 'state_mate/adapters/defaults'

module StateMate::Adapters::JSON
  include StateMate::Adapters
  register 'json'
  
  def self.parse_key key
    # use the same key seperation as Defaults
    StateMate::Adapters::Defaults.parse_key key
  end

  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

  def self.write key, value, options = {}
    options = {
      'pretty' => true,
    }.merge options

    filepath, key_segs = parse_key key

    new_root = if key_segs.length > 1
      root = read filepath

      StateMate::Adapters::Defaults.hash_deep_write!(
        root,
        key_segs,
        value
      )

      root
    else
      value
    end

    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
end # JSON

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
state_mate-0.0.7 lib/state_mate/adapters/json.rb
state_mate-0.0.6 lib/state_mate/adapters/json.rb