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



7
8
9
10
11
# File 'lib/mod_spox/BaseConfig.rb', line 7

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



15
16
17
18
19
# File 'lib/mod_spox/BaseConfig.rb', line 15

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



21
22
23
24
# File 'lib/mod_spox/BaseConfig.rb', line 21

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

#parse_configurationObject

Parses the configuration file into a usable Hash



27
28
29
30
31
32
33
34
# File 'lib/mod_spox/BaseConfig.rb', line 27

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



38
39
40
41
42
43
44
# File 'lib/mod_spox/BaseConfig.rb', line 38

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