Class: Config::Config

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

Constant Summary collapse

@@factoriesloaded =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_filename) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/module_config/config.rb', line 12

def initialize(_filename)
    unless @@factoriesloaded
        ItemFactory.load(File.join(File.dirname(__FILE__), "items"))
        @@factoriesloaded = true
    end
    
    @filename = _filename
    @sections = []
    file = IO.read(_filename)

    file.gsub!(/#.*$/, "")  # clear all the comments

    while !file.strip.empty?
        file = file.sub(Section.getRegex) do |match|
            begin
                @sections.push(Section.new(match, @filename))
                ""  # delete the proceded string (matched string will be sub'ed with the return value of this block)
            rescue ConfigException => e
                raise ConfigException.new(self.class), "  File:    #{_filename}\n#{e.message}"
            end
        end
    end
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/module_config/config.rb', line 10

def filename
  @filename
end

Instance Method Details

#[](_section) ⇒ Object



40
41
42
43
44
45
# File 'lib/module_config/config.rb', line 40

def [](_section)
    ifnotfound = lambda { raise Fhlow::FhlowException.new(@filename), "No such section. <#{_section}>" }

    @sections.detect(ifnotfound) { |section| section.name == _section }
        
end

#each(&block) ⇒ Object



36
37
38
# File 'lib/module_config/config.rb', line 36

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

#merge(_conf) ⇒ Object

merges _conf with self



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/module_config/config.rb', line 48

def merge(_conf)
    begin
        _conf.each do |arg_section| 
            if s = @sections.detect { |section| section.name == arg_section.name }
                s.merge(arg_section)
            else
                @sections.push(arg_section)
            end
        end         
    rescue ConfigException => e
        raise ConfigException.new(self.class), "  File:    #{_filename}\n#{e.message}"
    end
end

prints the configuration



63
64
65
66
# File 'lib/module_config/config.rb', line 63

def print()
    puts "File: #{@filename}"
    @sections.each { |s| s.print() }
end