Sha256: f77bd681c279b35c9468d43ed0b1600452b80d45b0276dba7db72c3dc72eae3b

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'yaml'

module Flow::Cli
  module Utils
    class DbManager
      class << self
        FLOW_CLI_CONFIG = "#{ENV['HOME']}/.flow_cli_config.yml".freeze

        def overide_save(hash)
          File.open(FLOW_CLI_CONFIG, "w") do |file|
            file << hash.to_yaml
          end
          hash
        end

        def save(settings)
          old = read
          settings = old.merge(settings).compact.stringify_keys
          yaml = settings.to_yaml
          File.open(FLOW_CLI_CONFIG, "w") do |file|
            file << yaml
          end
          settings
        end

        def save_attribute(key, val)
          dict = read
          dict[key.to_s] = val
          save(dict)
        end

        def read
          return {} unless File.file?(FLOW_CLI_CONFIG)
          config = YAML.safe_load(File.read(FLOW_CLI_CONFIG))
          raise "yaml load is not a hash #{config.class}" unless config.is_a? Hash
          config
        end

        def read_attribute(key)
          read[key.to_s]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flow-cli-0.0.4 lib/flow/cli/utils/db_manager.rb
flow-cli-0.0.3 lib/flow/cli/utils/db_manager.rb