Sha256: eb4d11f7497b2a567260f39b3af1fd3d69f920f9aa13edf437cfa0498b515a80

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'yao'

module Yao
  class Config
    def _configuration_defaults
      @_configuration_defaults ||= {}
    end

    def _configuration_hooks
      @_configuration_hooks ||= {}
    end

    def configuration
      @configuration ||= {}
    end

    def param(name, default, &hook)
      raise("Duplicate definition of #{name}") if self.respond_to?(name)

      name = name.to_sym
      _configuration_defaults[name] = default
      _configuration_hooks[name] = hook if block_given?
      self.define_singleton_method(name) do |*a|
        case a.size
        when 0
          configuration[name] || _configuration_defaults[name]
        when 1
          set(name, a.first)
        else
          raise ArgumentError, "wrong number of arguments (#{a.size} for 0, 1)"
        end
      end
    end

    def set(name, value)
      raise("Undefined config key #{name}") unless self.respond_to?(name)
      configuration[name.to_sym] = value
      _configuration_hooks[name].call(value) if _configuration_hooks[name]
      value
    end
  end

  def self.config(&blk)
    @__config ||= Config.new
    @__config.instance_eval(&blk) if blk
    @__config
  end

  def self.configure(&blk)
    config &blk
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yao-0.0.2.rc3 lib/yao/config.rb