Class: PingMon::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = PingMon::DEFAULT_CONFIG_FILE) ⇒ Config

Returns a new instance of Config.



8
9
10
11
# File 'lib/pingmon/config.rb', line 8

def initialize(config_file=PingMon::DEFAULT_CONFIG_FILE)
  @config_file = config_file
  @loaded = false
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



13
14
15
# File 'lib/pingmon/config.rb', line 13

def config_file
  @config_file
end

Class Method Details

.build_config(where = PingMon::DEFAULT_CONFIG_FILE) ⇒ Object



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

def self.build_config(where=PingMon::DEFAULT_CONFIG_FILE)
  from = File.dirname(__FILE__) + '/../../config/pingmon.yml'
  File.copy(File.expand_path(from), where)
  PingMon.Log << "Created configuration file '#{where}'." if PingMon.log

  where
end

Instance Method Details

#loadObject

Raises:



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

def load
  raise ConfigFileNotFound.new("Configuration file '#{config_file}' could not be found!") unless File.exists?(config_file)

  PingMon.log << "Loading configuration from '#{@config_file}'" if PingMon.log

  text = ERB.new(File.read(config_file)).result
  hash = YAML.load(text)

  base_config = (hash.symbolize_keys[:config]).stringify_keys
  base_config.keys.each do |key|
    instance_variable_set("@#{key}", base_config[key])
    self.class.class_eval do
      define_method(key.to_sym) { instance_variable_get("@#{key}") }
    end
  end
  @loaded = true

  self
end

#loaded?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/pingmon/config.rb', line 35

def loaded?
  @loaded
end