class Birdwatcher::Configuration

Constants

CONFIGURATION_FILE_LOCATION
CONFIGURATION_FILE_NAME

Public Class Methods

configured?() click to toggle source
# File lib/birdwatcher/configuration.rb, line 24
def self.configured?
  File.exist?(CONFIGURATION_FILE_LOCATION)
end
get(key) click to toggle source
# File lib/birdwatcher/configuration.rb, line 18
def self.get(key)
  self.instance.get!(key)
rescue Birdwatcher::Configuration::UnknownKey
  nil
end
get!(key) click to toggle source
# File lib/birdwatcher/configuration.rb, line 14
def self.get!(key)
  self.instance.get!(key)
end
load!() click to toggle source
# File lib/birdwatcher/configuration.rb, line 34
def self.load!
  self.instance.load_configuration!
end
save!(configuration) click to toggle source
# File lib/birdwatcher/configuration.rb, line 28
def self.save!(configuration)
  File.open(CONFIGURATION_FILE_LOCATION, "w") do |f|
    f.write(YAML.dump(configuration))
  end
end

Public Instance Methods

get!(key) click to toggle source
# File lib/birdwatcher/configuration.rb, line 50
def get!(key)
  key = key.to_sym
  fail(UnknownKey, "Unknown configuration key: #{key}") unless configuration.key?(key)
  configuration[key.to_sym]
end
load_configuration!() click to toggle source
# File lib/birdwatcher/configuration.rb, line 38
def load_configuration!
  if !File.exist?(CONFIGURATION_FILE_LOCATION)
    fail ConfigurationFileNotFound, "Configuration file does not exist"
  end
  if !File.readable?(CONFIGURATION_FILE_LOCATION)
    fail ConfigurationFileNotReadable, "Configuration file is not readable"
  end
  @configuration = YAML.load_file(CONFIGURATION_FILE_LOCATION).inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
rescue ::Psych::SyntaxError => e
  raise ConfigurationFileCorrupt, "Configuration file contains invalid YAML"
end

Private Instance Methods

configuration() click to toggle source
# File lib/birdwatcher/configuration.rb, line 58
def configuration
  load_configuration! unless @configuration
  @configuration
end