class ConfigFun

Public Class Methods

new(str = nil, &block) click to toggle source
# File lib/config_fun.rb, line 6
def initialize(str = nil, &block)
  @__config = {}
  @__cur = @__config
  @__parents = []
  instance_eval(&block) if block_given?
  instance_eval(str) if str
end
parse!(str = nil, &block) click to toggle source
# File lib/config_fun.rb, line 2
def self.parse!(str = nil, &block)
  self.new(str, &block).__config
end

Public Instance Methods

__config() click to toggle source
# File lib/config_fun.rb, line 72
def __config
  @__config
end
method_missing(m, *args, &block) click to toggle source
# File lib/config_fun.rb, line 14
def method_missing(m, *args, &block)
  m = (m.to_s[1..(m.to_s.size - 1)]).to_sym if m.to_s.index("_") == 0
  @__cur = @__parents.reduce(@__config) do |hash, cur|
    hash[cur].class == Array ? hash[cur].last : hash[cur]
  end

  if m == :partial
    read_partial(args)
  elsif block_given?
    $stderr << "Warning, ignoring args passed to a block.  args = '#{args}'\n" if args.count >= 1
    if (@__cur[m].nil? or @__cur[m].class == Hash) and (@__cur[m] == {} or not @__cur.keys.include?(m))
      @__parents << m
      @__cur[m] = {}
      @__cur = @__cur[m]
      instance_eval(&block)
      @__parents.pop
    elsif @__cur[m].class == Hash    # we've seen this element before, so convert to array.
      @__parents << m
      if @__parents.count > 1
        grandparent = @__parents[0..(@__parents.count - 2)]
        parent = @__parents[@__parents.count - 1]

        val = @__parents[0..(@__parents.count - 2)].reduce(@__config) do |hash, cur|
          hash[cur].class == Array ? hash[cur].last : hash[cur]
        end[parent]

        @__parents[0..(@__parents.count - 2)].reduce(@__config) do |hash, cur|
          hash[cur].class == Array ? hash[cur].last : hash[cur]
        end[parent] = [val]

        @__cur = @__parents[0..(@__parents.count - 2)].reduce(@__config)  do |hash, cur|
          hash[cur].class == Array ? hash[cur].last : hash[cur]
        end[parent]
      else
        # case where we are the first item
        @__config[m] = [@__config[m]]
        @__cur = @__config[m]
      end

      @__cur << {}
      instance_eval(&block)
      @__parents.pop
    elsif @__cur[m].class == Array   # already an array so just append our new element
      @__parents << m
      @__cur[m] << {}
      @__cur = @__cur[m].last
      instance_eval(&block)
      @__parents.pop
    end
  else
    if @__cur.class == Array
      @__cur.last[m] = args.first
    else
      @__cur[m] = args.first
    end
  end
end