class Announcer::Config

Attributes

name[R]

Public Class Methods

new(name=nil, &block) click to toggle source
# File lib/announcer/config.rb, line 29
def initialize(name=nil, &block)
  @name = name
  define(&block) if block_given?
end

Public Instance Methods

define(&block) click to toggle source
# File lib/announcer/config.rb, line 34
def define(&block)
  case block.arity
  when 0 then instance_eval(&block)
  when 1 then block.call(self)
  else   raise ConfigError, 'invalid config block arity'
  end
end
dup() click to toggle source

Deep dup all the values.

Calls superclass method
# File lib/announcer/config.rb, line 43
def dup
  super.tap do |new_config|
    new_config.instance_variable_set(
      :@_nested,
      Hash[
        _nested.map { |key, object|
          [
            key,
            object.is_a?(Config) ? object.dup : object
          ]
        }
      ]
    )
  end
end
merge_config_file!(file) click to toggle source
# File lib/announcer/config.rb, line 85
def merge_config_file!(file)
  merge_hash!(YAML.load_file(file))
end
merge_hash!(hash) click to toggle source
# File lib/announcer/config.rb, line 73
def merge_hash!(hash)
  hash.each { |k, v|
    if v.is_a?(Hash)
      send(k).merge_hash!(v)
    else
      send("#{k}=", v)
    end
  }

  self
end
method_missing(meth, *args, &block) click to toggle source
# File lib/announcer/config.rb, line 59
def method_missing(meth, *args, &block)
  meth_str = meth.to_s

  if /^(\w+)\=$/.match(meth_str)
    _set($1, *args, &block)
  elsif args.length > 0 || block_given?
    _add(meth, *args, &block)
  elsif /^(\w+)\?$/.match(meth_str)
    !!_get($1)
  else
    _get_or_create_namespace(meth)
  end
end

Private Instance Methods

_add(key, *args, &block) click to toggle source
# File lib/announcer/config.rb, line 109
def _add(key, *args, &block)
  raise NotAddableError, self.inspect if @_value && !@_value.is_a?(Array)
  object = _args_to_object(*args, &block)
  _set(key, object.is_a?(Proc) ? ProcArray.new : []) if !_get(key)
  _get(key).push(object)
end
_args_to_object(*args, &block) click to toggle source
# File lib/announcer/config.rb, line 116
def _args_to_object(*args, &block)
  if args.length == 1
    args.first
  elsif args.length > 1
    args
  elsif block_given?
    block
  else
    raise ConfigError, 'must pass value or block'
  end
end
_get(key) click to toggle source
# File lib/announcer/config.rb, line 90
def _get(key)
  _nested[key.to_sym]
end
_get_or_create_namespace(key) click to toggle source
# File lib/announcer/config.rb, line 94
def _get_or_create_namespace(key)
  object = _get(key)

  if object.is_a?(UndefinedValue)
    object = _set(key, Config.new((name ? "#{name}." : '') + key.to_s))
  end

  object
end
_nested() click to toggle source
# File lib/announcer/config.rb, line 128
def _nested
  @_nested ||= Hash.new { |hash, key| hash[key] = UndefinedValue.new }
end
_set(key, *args, &block) click to toggle source
# File lib/announcer/config.rb, line 104
def _set(key, *args, &block)
  object = _args_to_object(*args, &block)
  _nested[key.to_sym] = object
end