Class: FlexConf
- Inherits:
-
Object
- Object
- FlexConf
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/flexconf.rb
Overview
A simple but flexible configuration class.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value for the given key, which can be a string or a symbol.
-
#has_key?(key) ⇒ Boolean
Returns true if the given configuration value exists.
-
#initialize(source = nil, options = nil) ⇒ FlexConf
constructor
Loads configuration from the supplied source(s).
Constructor Details
#initialize(hash) ⇒ FlexConf #initialize(yaml_file, opts) ⇒ FlexConf #initialize ⇒ FlexConf
Loads configuration from the supplied source(s).
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/flexconf.rb', line 50 def initialize(source=nil, =nil) @data = {} case source when /.*\.yml/ source_data = YAML.load_file(source) if && [:scope] flexify scoped(source_data, [:scope]) else flexify source_data end handle_overrides(source, ) if when Hash flexify source when nil if File.exists?('config.yml') initialize('config.yml', :local => 'config_local.yml', :environment => true) else raise ArgumentError, "FlexConf can't load: no configuration was given and there is no config.yml file to default to." end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
184 185 186 |
# File 'lib/flexconf.rb', line 184 def method_missing(name, *args, &block) self.has_key?(name) ? self[name] : super end |
Instance Method Details
#[](key) ⇒ Object
Returns the value for the given key, which can be a string or a symbol. Named keys can also be accessed via method calls (i.e. ‘config.foo`). Nested configurations are returned as FlexConf objects.
75 76 77 |
# File 'lib/flexconf.rb', line 75 def [](key) @data[normalize_key(key)] end |
#has_key?(key) ⇒ Boolean
Returns true if the given configuration value exists.
80 81 82 |
# File 'lib/flexconf.rb', line 80 def has_key?(key) @data.has_key? normalize_key(key) end |