Class: ModSpox::BaseConfig

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

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ BaseConfig

file_path

path to configuration file

Creates new BaseConfig



9
10
11
12
13
# File 'lib/mod_spox/BaseConfig.rb', line 9

def initialize(file_path)
    @config = Hash.new
    @file_path = file_path
    parse_configuration
end

Instance Method Details

#[](name) ⇒ Object

name

key of config item wanted

Provides access to configuration data



17
18
19
20
21
# File 'lib/mod_spox/BaseConfig.rb', line 17

def [](name)
    name = name.to_sym unless name.is_a?(Symbol)
    raise Exceptions::UnknownKey.new("Configuration has no value named: #{name.to_s}") unless @config.has_key?(name)
    return @config[name]
end

#[]=(key, value) ⇒ Object



23
24
25
26
27
# File 'lib/mod_spox/BaseConfig.rb', line 23

def []=(key, value)
    key = key.to_sym unless key.is_a?(Symbol)
    @config[key] = value
    write_configuration
end

#parse_configurationObject

Parses the configuration file into a usable Hash



30
31
32
33
34
35
36
37
# File 'lib/mod_spox/BaseConfig.rb', line 30

def parse_configuration
    return unless File.exists?(BotConfig[:userconfigpath])
    IO.readlines(BotConfig[:userconfigpath]).each{|line|
        if(line =~ /^(\S+)\s*=\s*(\S*)\s*$/)
            @config[$1.to_sym] = $2
        end
    }
end

#write_configurationObject

Writes the configuration file out to the provided file_path during initialization



41
42
43
44
45
46
47
# File 'lib/mod_spox/BaseConfig.rb', line 41

def write_configuration
    file = File.open(BotConfig[:userconfigpath], 'w')
    @config.each_pair{|k,v|
        file.puts("#{k.to_s}=#{v}")
    }
    file.close
end