Class: AwesomeConf
- Inherits:
-
Object
- Object
- AwesomeConf
- Defined in:
- lib/awesome_conf.rb
Constant Summary collapse
- @@strict =
false
Instance Method Summary collapse
-
#initialize(hash) ⇒ AwesomeConf
constructor
Create a new conf object You can instantiate a conf object with an Hash Or with the path of a YAML file.
- #method_missing(m) ⇒ Object
-
#strict! ⇒ Object
Set to strict in order to raise when using an unfound conf variable.
Constructor Details
#initialize(hash) ⇒ AwesomeConf
Create a new conf object You can instantiate a conf object with an Hash Or with the path of a YAML file
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/awesome_conf.rb', line 9 def initialize(hash) begin hash = YAML::load_file(hash) if String === hash rescue Exception => e raise e.class.new("AwesomeConf: Error loading yaml file: #{hash} (#{e})") end m = Module.new do instance_methods.each { |selector| remove_method(selector) } hash.each do |k, v| const_set('AC_' + k.to_s, v) module_eval <<-END_EVAL, __FILE__, __LINE__ + 1 def #{k} #{'AC_'+ k.to_s} end END_EVAL end end extend m freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m) ⇒ Object
41 42 43 44 |
# File 'lib/awesome_conf.rb', line 41 def method_missing(m) raise "AwesomeConf: trying to access an inexisting configuration variable in a strict configuration environment: #{m}" if @@strict nil end |
Instance Method Details
#strict! ⇒ Object
Set to strict in order to raise when using an unfound conf variable
36 37 38 39 |
# File 'lib/awesome_conf.rb', line 36 def strict! @@strict = true self end |