Class: Config::Section

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

Constant Summary collapse

@@beginName =
'^\s*[\[]'
@@name =
'[\w\s]+'
@@endName =
'[\]]\s*$'
@@regex =
/#{@@beginName}#{@@name}#{@@endName}[^\[]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_str, _filename) ⇒ Section

Returns a new instance of Section.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/module_config/section.rb', line 15

def initialize(_str, _filename)
    @filename = _filename
    @items = []
    str = _str.sub(/#{@@beginName}(#{@@name})#{@@endName}/, '')
    @name = $1


    while !str.strip.empty?
        begin 
            @items.push(ItemFactory.getItemFor(str))    # the matched string will be deleted from str by ItemFactory.getItemFor()

        rescue ConfigException => e
            raise ConfigException.new(self.class), "  Section: #{@name}\n  Error:   #{e.message}"
        end
    end

end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/module_config/section.rb', line 12

def name
  @name
end

Class Method Details

.getRegexObject



75
76
77
# File 'lib/module_config/section.rb', line 75

def Section.getRegex()
    @@regex
end

Instance Method Details

#[](_name) ⇒ Object



48
49
50
51
52
# File 'lib/module_config/section.rb', line 48

def [](_name)
    ifnotfound = lambda { raise Fhlow::FhlowException.new("Config::Section"), "#{@filename}\n       No such item <#{_name}> in section <#{@name}>" }
    i = @items.detect(ifnotfound) { |item| item.name == _name }
    i.value
end

#each(&block) ⇒ Object



33
34
35
# File 'lib/module_config/section.rb', line 33

def each(&block)
    @items.each(&block)
end

#getItem(_name) ⇒ Object



43
44
45
46
# File 'lib/module_config/section.rb', line 43

def getItem(_name)
    ifnotfound = lambda { raise Fhlow::FhlowException.new("Config::Section"), "#{@filename}\n       No such item <#{_name}> in section <#{@name}>" }
    @items.detect(ifnotfound) { |item| item.name == _name }
end

#item_each(_name, &_block) ⇒ Object



37
38
39
40
41
# File 'lib/module_config/section.rb', line 37

def item_each(_name, &_block)
    ifnotfound = lambda { raise Fhlow::FhlowException.new("Config::Section"), "#{@filename}\n        No such item <#{_name}> in section <#{@name}>" }
    i = @items.detect(ifnotfound) { |item| item.name == _name }
    i.each(&_block)
end

#merge(_section) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/module_config/section.rb', line 54

def merge(_section)
    begin
        _section.each do |arg_item|
            if i = @items.detect { |item| item.name == arg_item.name }
                i.merge(arg_item)
            else
                @items.push(arg_item)
            end
        end
    
    rescue ConfigException => e
        raise ConfigException.new(self.class), "  Section: #{@name}\n  Error:   #{e.message}"
        
    end
end


70
71
72
73
# File 'lib/module_config/section.rb', line 70

def print()
    puts "[#{@name}]"
    @items.each { |i| i.print() }
end