class RubyConf::Config

Attributes

__rc_attributes[R]
__rc_chains[R]
__rc_locked[R]
__rc_name[R]
__rc_parent[R]
__rc_static_values[R]

Public Class Methods

new(name = nil, parent = nil, &block) click to toggle source
# File lib/ruby-conf.rb, line 91
def initialize(name = nil, parent = nil, &block)
  @__rc_locked, @__rc_attributes, @__rc_chains, @__rc_parent, @__rc_static_values = false, {}, [], parent, {}
  @__rc_name = name.to_sym if name
  instance_eval(&block) if block_given?
  __rc_lock
end

Public Instance Methods

[](name, *args) click to toggle source
# File lib/ruby-conf.rb, line 110
def [](name, *args)
  name = name.to_sym
  value = @__rc_attributes[name]
  if value.is_a?(Proc)
    args += [nil] * (value.arity.abs - args.size) if value.arity.abs > args.size
    @@stack ||= []
    if @@stack.include?(name)
      RubyConf.err("[ruby-conf] Detected recursive proc: #{name}")
      "[RECURSIVE]"
    else
      @@stack << name
      begin
        value = __rc_root.instance_exec(*args, &value)
        static_key = "#{name}:#{args}"
        @__rc_static_values[static_key] = value
        value
      ensure
        @@stack.delete(name)
      end
    end
  else
    value
  end
end
[]=(name, value) click to toggle source
# File lib/ruby-conf.rb, line 147
def []=(name, value)
  @__rc_attributes[name.to_sym] = value
end
__rc_build_inspect() click to toggle source
# File lib/ruby-conf.rb, line 257
def __rc_build_inspect()
  istr = ""
  istr += "[#{@__rc_name || "CONFIG"}] " unless @__rc_parent
  istr += @__rc_attributes.keys.map{|k| k.to_s }.sort.map{ |key|
    str = ""
    value = begin
      self[key]
    rescue => e
      "[UNRESOLVED:#{e}]"
    end
    str += "#{key}: "
    str += value.is_a?(RubyConf::Config) ? (value == self ? "[SELF]" : "{ #{value.__rc_build_inspect} }") : value.inspect
    str
  }.join(", ")
  istr
end
__rc_build_string(depth = 0) click to toggle source
# File lib/ruby-conf.rb, line 240
def __rc_build_string(depth = 0)
  str = ""
  str += "[#{@__rc_name || "CONFIG"}]\n" unless @__rc_parent
  str += "\n"
  @__rc_attributes.keys.map{|k| k.to_s }.sort.each do |key|
    value = begin
      self[key]
    rescue => e
      "[UNRESOLVED]"
    end

    str += "  " * depth
    str += "#{key}:"
    str += value.is_a?(Config) ? (value == self ? " [SELF]\n" : value.__rc_build_string(depth+1)) : " #{value}\n"
  end
  str
end
__rc_copy(o) click to toggle source
# File lib/ruby-conf.rb, line 135
def __rc_copy(o)
  if o.is_a?(Hash)
    o.inject({}) { |copy, (key, val)| copy[key] = __rc_copy(val); copy }
  elsif o.is_a?(Array)
    o.inject([]) { |copy, i| copy << __rc_copy(i); copy }
  else
    o
  end
end
__rc_lock() click to toggle source
# File lib/ruby-conf.rb, line 98
def __rc_lock
  @__rc_locked = true
  @__rc_attributes.each_value { |value| value.__rc_lock if value.is_a?(Config) }
  self
end
__rc_root() click to toggle source
# File lib/ruby-conf.rb, line 78
def __rc_root() __rc_parent ? __rc_parent.__rc_root : self end
__rc_set_defaults(conf, key, value) click to toggle source
# File lib/ruby-conf.rb, line 151
def __rc_set_defaults(conf, key, value)
  if conf.__rc_attributes.key?(key)
    if value.is_a?(Config)
      sub = conf.__rc_attributes[key]
      value.__rc_attributes.each do |k, v|
        __rc_set_defaults(sub, k, v)
      end
    end
  else
    conf.__rc_attributes[key] = value
  end
end
__rc_static_proc(name, *args) click to toggle source
# File lib/ruby-conf.rb, line 104
def __rc_static_proc(name, *args)
  name = name.to_sym
  static_key = "#{name}:#{args}"
  @__rc_static_values[static_key] || self[name, *args]
end
detach(parent = nil) click to toggle source
# File lib/ruby-conf.rb, line 79
def detach(parent = nil)
  @__rc_parent = parent
  @__rc_attributes.values.each do |child|
    child.detach(self) if child.is_a?(Config)
  end
  self
end
inspect() click to toggle source
# File lib/ruby-conf.rb, line 276
def inspect() __rc_build_inspect end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/ruby-conf.rb, line 164
def method_missing(method_name, *args, &block)
  name, modifier = method_name.to_s.match(/^(?<name>.*?)(?<modifier>[?!])?$/).captures
  name = name.to_sym

  options = args.last.is_a?(Hash) ? args.last : {}

  if block_given?
    if options.key?(:inherits)
      self[name] = [*options[:inherits]].inject(Config.new(name, self, &block)) do |conf, inherited|
        inherited = self[inherited.to_sym] unless inherited.is_a?(Config)
        __rc_copy(inherited.__rc_attributes).each do |key, value|
          __rc_set_defaults(conf, key, value)
        end
        conf
      end
    elsif self[name].is_a?(Config)
      self[name].instance_eval(&block)
    else
      self[name] = Config.new(name, self, &block)
    end

    while (chain = self[name].__rc_chains.pop)
      self[name][chain] = chain.__rc_chain.nil? ?  "" : chain.__rc_chain.__rc_gather
    end

  else

    if @__rc_locked && args.size == 0 && !@__rc_attributes.key?(name)
      return case modifier
        when '!'
          super
        when '?'
          false
        else
          nil
      end
    end

    if !@__rc_attributes.key?(name) && (args.size == 0 || args.first.is_a?(Magic))
      str = name.to_s
      str.extend(Magic)
      if args.first.is_a?(Magic)
        str.__rc_chain = args.first
        @__rc_chains.delete_if { |a| a.equal? args.first }
      end
      @__rc_chains << str
      return str
    end

    if args.empty?
      if modifier == '?'
        !!self[name]
      elsif modifier == '!' && __rc_attributes[name.to_sym].is_a?(Proc)
        __rc_static_proc(name)
      else
        self[name]
      end
    else
      arg = args.size == 1 ? args.first : args
      if @__rc_locked && __rc_attributes[name.to_sym].is_a?(Proc)
        if modifier == '!'
          __rc_static_proc(name, *args)
        else
          self[name, *args]
        end
      else
        self[name] = arg
      end
    end
  end
end
rckeys() click to toggle source
# File lib/ruby-conf.rb, line 87
def rckeys
  @__rc_attributes.keys
end
respond_to?(name) click to toggle source
Calls superclass method
# File lib/ruby-conf.rb, line 236
def respond_to?(name)
  super || @__rc_attributes.key?(name) || @__rc_parent.respond_to?(name)
end
to_a() click to toggle source
# File lib/ruby-conf.rb, line 145
def to_a() [self] end
to_s() click to toggle source
# File lib/ruby-conf.rb, line 274
def to_s() __rc_build_string end
to_str() click to toggle source
# File lib/ruby-conf.rb, line 275
def to_str() to_s end