Sha256: 70fa331d8b39ffa46f27a949f4c45ef2d1c20269edd2c7d627dcaf2320d73a82

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

require 'yaml'

module Donjon
  class Settings
    attr_reader :path

    def initialize(path = nil)
      @path = path || _default_path
      @data = nil
    end

    def configured?
      user_name && private_key && vault_path
    end

    def method_missing(method_name, *args, &block)
      if method_name.to_s.end_with?('=')
        set(method_name.to_s.chop, *args)
      else
        get(method_name.to_s, *args)
      end
    end

    def respond_to?(method_name)
      !!(method_name.to_s =~ /[a-z][a-z_]*=?/)
    end

    private

    def get(key)
      @data ||= _load
      @data[key]
    end

    def set(key, value)
      @data ||= _load
      @data[key] = value
      _save(@data)
      value
    end

    def _load
      @path.exist? ? YAML.load_file(@path) : {}
    end
    
    def _save(data)
      @data['timestamp'] = Time.now
      @path.parent.mkpath
      @path.write data.to_yaml
    end

    def _fallback_path
      Pathname.new('~').join('.donjonrc').expand_path
    end

    def _default_path
      if ENV.fetch('DONJONRC', nil)
        Pathname.new(ENV.fetch('DONJONRC')).expand_path
      else
        _fallback_path
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
donjon-2.0.1 lib/donjon/settings.rb
donjon-2.0.0 lib/donjon/settings.rb
donjon-1.0.1 lib/donjon/settings.rb
donjon-1.0.0 lib/donjon/settings.rb
donjon-0.0.5 lib/donjon/settings.rb
donjon-0.0.4 lib/donjon/settings.rb
donjon-0.0.3 lib/donjon/settings.rb
donjon-0.0.2 lib/donjon/settings.rb