Class: ConfigFun

Inherits:
Object
  • Object
show all
Defined in:
lib/config_fun.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil, &block) ⇒ ConfigFun

Returns a new instance of ConfigFun.



6
7
8
9
10
11
12
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

Class Method Details

.parse!(str = nil, &block) ⇒ Object



2
3
4
# File 'lib/config_fun.rb', line 2

def self.parse!(str = nil, &block)
  self.new(str, &block).__config
end

Instance Method Details

#__configObject



72
73
74
# File 'lib/config_fun.rb', line 72

def __config
  @__config
end