Module: ConfParser::Template

Included in:
ConfParser, Section
Defined in:
lib/confparser.rb

Class Method Summary collapse

Class Method Details

.included(obj) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/confparser.rb', line 17

def self.included (obj)
  obj.class_eval {
    attr_reader :parent
    alias __get__ []
    alias __set__ []=
    private :__get__, :__set__

    def [] (name)
      __get__(name).tap {|x|
        break x.gsub(/\$\((.+?)\)/) {|n|
          (self[$1] || parent[$1]).to_s
        }.strip if x.is_a?(String)
      }
    end

    def to_s (separator = ' = ')
      map {|key, value|
        value.is_a?(Hash) ? "[#{key}]\n#{value.to_s(separator).gsub(/^/, '  ')}" : "#{key}#{separator}#{value.to_s}"
      }.join("\n")
    end
  }
end