Sha256: 8bca6115ac7de37a25ac3d785f9ab9580714e6d14e76662a390059083b52171a

Contents?: true

Size: 1.52 KB

Versions: 25

Compression:

Stored size: 1.52 KB

Contents

require 'fileutils'
require 'json'

module Stove
  class Config
    include Logify
    include Mixin::Instanceable

    def method_missing(m, *args, &block)
      if m.to_s.end_with?('=')
        __set__(m.to_s.chomp('='), args.first)
      else
        __get__(m)
      end
    end

    def respond_to_missing?(m, include_private = false)
      __has__?(m) || super
    end

    def save
      FileUtils.mkdir_p(File.dirname(__path__))
      File.open(__path__, 'w') do |f|
        f.write(JSON.fast_generate(__raw__))
      end
    end

    def to_s
      "#<#{self.class.name} #{__raw__.to_s}>"
    end

    def inspect
      "#<#{self.class.name} #{__raw__.inspect}>"
    end

    def __get__(key)
      __raw__[key.to_sym]
    end

    def __has__?(key)
      __raw__.key?(key.to_sym)
    end

    def __set__(key, value)
      __raw__[key.to_sym] = value
    end

    def __unset__(key)
      __raw__.delete(key.to_sym)
    end

    def __path__
      @path ||= File.expand_path(ENV['STOVE_CONFIG'] || '~/.stove')
    end

    def __raw__
      return @__raw__ if @__raw__

      @__raw__ = JSON.parse(File.read(__path__), symbolize_names: true)

      if @__raw__.key?(:community)
        $stderr.puts "Detected old Stove configuration file, converting..."

        @__raw__ = {
          :username => @__raw__[:community][:username],
          :key      => @__raw__[:community][:key],
        }
      end

      @__raw__
    rescue Errno::ENOENT => e
      log.warn { "No config file found at `#{__path__}'!" }
      @__raw__ = {}
    end
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
stove-3.2.4 lib/stove/config.rb
stove-3.2.3 lib/stove/config.rb
stove-3.2.2 lib/stove/config.rb
stove-3.2.1 lib/stove/config.rb
stove-3.0.0 lib/stove/config.rb