Sha256: e669501efc4fa3ff63921cacb6a2c9cbecd8b1d8fed9d37f799c6649b0184941

Contents?: true

Size: 642 Bytes

Versions: 2

Compression:

Stored size: 642 Bytes

Contents

require "yaml"

module Cp8Cli
  class ConfigStore
    def initialize(path)
      @path = path
    end

    def [](key)
      data[key]
    end

    def exist?
      File.exist?(path)
    end

    def move_to(new_path)
      File.rename(path, new_path)
      @path = new_path
    end

    def save(key, value)
      data[key] = value
      File.new(path, "w") unless exist?
      File.open(path, "w") { |f| f.write(data.to_yaml) }
      value
    end

    private

      attr_reader :path

      def data
        @_data ||= load_data
      end

      def load_data
        YAML.load File.read(path)
      rescue
        {}
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cp8_cli-9.1.1 lib/cp8_cli/config_store.rb
cp8_cli-9.1.0 lib/cp8_cli/config_store.rb