Class: Res::Config
- Inherits:
-
Object
- Object
- Res::Config
- Defined in:
- lib/res/config.rb
Instance Attribute Summary collapse
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#prepend ⇒ Object
Returns the value of attribute prepend.
-
#required ⇒ Object
Returns the value of attribute required.
-
#struct ⇒ Object
Returns the value of attribute struct.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(items, args = {}) ⇒ Config
constructor
A new instance of Config.
- #load_from_config(config_file) ⇒ Object
- #method_missing(m, *args, &block) ⇒ Object
-
#process(args = {}) ⇒ Object
Load in config – this can come from three places: 1.
Constructor Details
#initialize(items, args = {}) ⇒ Config
Returns a new instance of Config.
8 9 10 11 12 13 14 |
# File 'lib/res/config.rb', line 8 def initialize(items, args = {}) @required = *items @optional = args[:optional] || [] @prepend = args[:pre_env] || '' items = required + optional @struct = Struct.new(*items).new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
54 55 56 |
# File 'lib/res/config.rb', line 54 def method_missing(m, *args, &block) struct.send(m,*args, &block) end |
Instance Attribute Details
#optional ⇒ Object
Returns the value of attribute optional.
6 7 8 |
# File 'lib/res/config.rb', line 6 def optional @optional end |
#prepend ⇒ Object
Returns the value of attribute prepend.
6 7 8 |
# File 'lib/res/config.rb', line 6 def prepend @prepend end |
#required ⇒ Object
Returns the value of attribute required.
6 7 8 |
# File 'lib/res/config.rb', line 6 def required @required end |
#struct ⇒ Object
Returns the value of attribute struct.
6 7 8 |
# File 'lib/res/config.rb', line 6 def struct @struct end |
Class Method Details
.symbolize_keys(hash) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/res/config.rb', line 46 def self.symbolize_keys(hash) symbolized_hash = {} hash.each do |key, value| symbolized_hash[key.to_sym] = value end symbolized_hash end |
Instance Method Details
#load_from_config(config_file) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/res/config.rb', line 36 def load_from_config(config_file) config = {} if File.exists?(config_file) config = Res::Config.symbolize_keys(YAML::load(File.open(config_file))) else raise "Couldn't find config file '#{config_file}'" end config end |
#process(args = {}) ⇒ Object
Load in config – this can come from three places:
-
Arguments passed to the initializer
-
From environment variables
-
From a config file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/res/config.rb', line 20 def process( args = {} ) config_from_file = {} if args[:config_file] config_from_file = load_from_config(args[:config_file]) args.delete(:config_file) end missing = [] struct.members.each do |item| struct[item] = args[item] || ENV[(prepend + item.to_s).upcase] || config_from_file[item] || nil missing << item if (struct[item].nil? && required.include?(item)) end raise "Missing configuration: " + missing.join( ", ") if missing.any? end |