class UltraConfig::Namespace

Attributes

logger[RW]
objects[R]

Public Class Methods

new(parents = [], &block) click to toggle source
# File lib/ultra_config/namespace.rb, line 12
def initialize(parents = [], &block)
  @configuration = [block]
  @parents = parents

  reset
end

Public Instance Methods

config(name, options = {}, &block) click to toggle source
# File lib/ultra_config/namespace.rb, line 33
def config(name, options = {}, &block)
  @objects[name] = Config.new(name, @parents, options, &block)
  define_singleton_method("#{name}=") { |value| @objects[name].value = value }
  define_singleton_method(name) { @objects[name].value }
end
extend(&block) click to toggle source
# File lib/ultra_config/namespace.rb, line 19
def extend(&block)
  @configuration << block
  self.instance_eval(&block)
end
helper(name, &block) click to toggle source
# File lib/ultra_config/namespace.rb, line 39
def helper(name, &block)
  define_singleton_method(name, &block)
end
merge_hash!(hash, parents = []) click to toggle source
# File lib/ultra_config/namespace.rb, line 61
def merge_hash!(hash, parents = [])
  hash.each do |k, v|
    options = send_chain(parents)
    if options.objects[k.to_sym].is_a?(Config)
      options.send("#{k}=", v) unless v.nil?
    elsif options.objects[k.to_sym].is_a?(Namespace)
      merge_hash!(v, parents + [k])
    else
      logger.warn { "received an unknown config #{k} with value #{v} and parents: #{parents}" }
    end
  end

  self
end
method_missing(m) click to toggle source
# File lib/ultra_config/namespace.rb, line 93
def method_missing(m)
  raise ObjectNotFoundError
end
namespace(name, &block) click to toggle source
# File lib/ultra_config/namespace.rb, line 28
def namespace(name, &block)
  @objects[name] = Namespace.new(@parents + [name], &block)
  define_singleton_method(name) { @objects[name] }
end
reset() click to toggle source
# File lib/ultra_config/namespace.rb, line 43
def reset
  @objects = {}
  @configuration.each { |config| self.instance_eval(&config) }
end
setting(name, value) click to toggle source
# File lib/ultra_config/namespace.rb, line 24
def setting(name, value)
  Settings.set(name, value)
end
to_h() click to toggle source
# File lib/ultra_config/namespace.rb, line 48
def to_h
  hash = {}
  @objects.each do |name, object|
    if object.is_a?(Config)
      hash[name] = object.value
    else
      hash[name] = object.to_h
    end
  end

  hash
end
to_sanitized_h() click to toggle source
# File lib/ultra_config/namespace.rb, line 76
def to_sanitized_h
  hash = {}
  @objects.each do |name, object|
    if object.is_a?(Config)
      if object.sanitize?
        hash[name] = object.value.nil? ? nil : '*****'
      else
        hash[name] = object.value
      end
    else
      hash[name] = object.to_sanitized_h
    end
  end

  hash
end

Private Instance Methods

send_chain(arr) click to toggle source

Send a chain of methods to an object @param arr [Array] list of methods to send to object @return [Object] result of method chain

# File lib/ultra_config/namespace.rb, line 102
def send_chain(arr)
  arr.inject(self) {|obj, arr| obj.send(arr) }
end