Sha256: f3c8918a027b574e7fb88cb5f28aaa7c57879cdfb6cea669ce702d86351c5e1d

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

require 'json'

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

module StateMate::Adapters::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

4 entries across 4 versions & 1 rubygems

Version Path
state_mate-0.0.5 lib/state_mate/adapters/json.rb
state_mate-0.0.4 lib/state_mate/adapters/json.rb
state_mate-0.0.3 lib/state_mate/adapters/json.rb
state_mate-0.0.2 lib/state_mate/adapters/json.rb