Sha256: bfc922e90d93b0e23ffe2f22123d0470c87c3815a385b730f3019c3853cbb216

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

require "tamashii/environment"

module Tamashii
  class Config < Hash
    SHARED_CONFIG = %i(auth_type token log_file log_level token env).freeze

    class << self
      attr_accessor :default_config

      def method_missing(name, *args, &block)
        (@instance ||= self.new).send(name, *args, &block)
      end

      def register(name, default = nil)
        self.default_config ||= {}
        self.default_config[name.to_sym] = default
      end

      def [](name)
        send(name.to_sym)
      end
    end

    def initialize
      @accept_config = Set.new(SHARED_CONFIG)
      (self.class.default_config || {}).each { |name, value| register(name, value) }
    end

    def method_missing(name, *args, &block)
      return unless accept?(name) || accept?(name[0..-2])
      return register(name[0..-2], args.first) if name[-1..-1] == "="
      return register(name, args.first) if args.size > 0
      return register(name, yield(self)) if block_given?
      self[name]
    end

    def register(key, default = nil)
      @accept_config.add(key.to_sym)
      self[key.to_sym] = default if default
    end

    def accept?(key)
      @accept_config.include?(key.to_sym)
    end

    def env(env = nil)
      return Environment.new(self[:env]) if env.nil?
      self[:env] = env.to_s
    end

    def env=(env)
      self[:env] = env.to_s
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tamashii-common-0.1.6 lib/tamashii/config.rb
tamashii-common-0.1.5 lib/tamashii/config.rb
tamashii-common-0.1.4 lib/tamashii/config.rb
tamashii-common-0.1.3 lib/tamashii/config.rb
tamashii-common-0.1.2 lib/tamashii/config.rb