Class: Syndi::Config
- Inherits:
-
Object
- Object
- Syndi::Config
- Defined in:
- lib/syndi/config.rb
Overview
A class which provides a functional, simple configuration interface. It uses YAML by means of Psych.
Instance Attribute Summary collapse
-
#conf ⇒ Hash{}
This is the hash which contains the data parsed from the configuration file.
- #type ⇒ Object readonly
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return value of @conf[key].
-
#initialize(filepath) ⇒ Config
constructor
Produce a new instance, and attempt to parse.
-
#parse ⇒ Object
private
Parse the configuration file, and output the data to #x.
-
#rehash! ⇒ Object
Rehash the configuration.
Constructor Details
Instance Attribute Details
#conf ⇒ Hash{}
Returns This is the hash which contains the data parsed from the configuration file.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/syndi/config.rb', line 23 class Config attr_reader :conf, :type # Produce a new instance, and attempt to parse. # # @param [String] filepath Path to configuration file. def initialize filepath $m.verbose("Trying to initialize configuration from '#{filepath}'...", VSIMPLE) do @path = filepath parse end # verbose end # Rehash the configuration. # # If an error occurs, it will revert the configuration to its prior state # so that everything can continue to function. def rehash! $m.debug("Configuration file is rehashing.") # Keep the old configuration in case of issues. oldconf = @conf @conf = {} # Rehash parse! # Ensure it really succeeded. if @conf.empty? or !@conf.instance_of? Hash # Nope. Restore old configuration. @conf = oldconf $m.error 'Failed to rehash the configuration file (parser produced empty config)! Reverting to old configuration.' return 0 end $m.events.call :rehash # This rescue is applicable to anything that happens in here, since if it is reached, there really was an error in general. rescue => e $m.error 'Failed to rehash configuration file! Reverting to old configuration.', false, e.backtrace @conf = oldconf return 0 end # Return value of @conf[key]. # # @return [Object] Value of @conf[key]. # @see @conf def [] key @conf[key] end ####### private ####### # Parse the configuration file, and output the data to {#x}. # # @raise [ConfigError] If the file does not exist. # @raise [ConfigError] If the file cannot be processed. def parse # Ensure foremost that the configuration file exists. unless File.exists? @path raise ConfigError, "Configuration file '#@path' does not exist!" end # Get the data from the file. f = File.open(@path) data = f.read f.close conf = {} # Process the YAML. begin conf = Psych.load data rescue => e raise ConfigError, "Failed to process the YAML in '#@path'", e.backtrace end @conf = conf end # def parse end |
#type ⇒ Object (readonly)
25 26 27 |
# File 'lib/syndi/config.rb', line 25 def type @type end |
Instance Method Details
#[](key) ⇒ Object
Return value of @conf[key].
73 74 75 |
# File 'lib/syndi/config.rb', line 73 def [] key @conf[key] end |
#parse ⇒ Object (private)
Parse the configuration file, and output the data to #x.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/syndi/config.rb', line 85 def parse # Ensure foremost that the configuration file exists. unless File.exists? @path raise ConfigError, "Configuration file '#@path' does not exist!" end # Get the data from the file. f = File.open(@path) data = f.read f.close conf = {} # Process the YAML. begin conf = Psych.load data rescue => e raise ConfigError, "Failed to process the YAML in '#@path'", e.backtrace end @conf = conf end |
#rehash! ⇒ Object
Rehash the configuration.
If an error occurs, it will revert the configuration to its prior state so that everything can continue to function.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/syndi/config.rb', line 41 def rehash! $m.debug("Configuration file is rehashing.") # Keep the old configuration in case of issues. oldconf = @conf @conf = {} # Rehash parse! # Ensure it really succeeded. if @conf.empty? or !@conf.instance_of? Hash # Nope. Restore old configuration. @conf = oldconf $m.error 'Failed to rehash the configuration file (parser produced empty config)! Reverting to old configuration.' return 0 end $m.events.call :rehash # This rescue is applicable to anything that happens in here, since if it is reached, there really was an error in general. rescue => e $m.error 'Failed to rehash configuration file! Reverting to old configuration.', false, e.backtrace @conf = oldconf return 0 end |