Class: ModSpox::BaseConfig
- Inherits:
-
Object
- Object
- ModSpox::BaseConfig
- Defined in:
- lib/mod_spox/BaseConfig.rb
Instance Method Summary collapse
-
#[](name) ⇒ Object
- name
-
key of config item wanted Provides access to configuration data.
- #[]=(key, value) ⇒ Object
-
#initialize(file_path) ⇒ BaseConfig
constructor
- file_path
-
path to configuration file Creates new BaseConfig.
-
#parse_configuration ⇒ Object
Parses the configuration file into a usable Hash.
-
#write_configuration ⇒ Object
Writes the configuration file out to the provided file_path during initialization.
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_configuration ⇒ Object
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_configuration ⇒ Object
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 |