Class: Appserver::Configurator
- Inherits:
-
Struct
- Object
- Struct
- Appserver::Configurator
- Defined in:
- lib/appserver/configurator.rb
Instance Attribute Summary collapse
-
#context_keys ⇒ Object
Returns the value of attribute context_keys.
-
#global_keys ⇒ Object
Returns the value of attribute global_keys.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #apply!(target, context = nil) ⇒ Object
- #context(context) ⇒ Object (also: #app)
-
#initialize(config_file, global_keys = nil, context_keys = nil) ⇒ Configurator
constructor
A new instance of Configurator.
Constructor Details
#initialize(config_file, global_keys = nil, context_keys = nil) ⇒ Configurator
Returns a new instance of Configurator.
4 5 6 7 8 9 |
# File 'lib/appserver/configurator.rb', line 4 def initialize (config_file, global_keys = nil, context_keys = nil) self.settings = {} self.global_keys = global_keys self.context_keys = context_keys instance_eval(File.read(config_file), config_file) if config_file end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (protected)
31 32 33 34 35 36 |
# File 'lib/appserver/configurator.rb', line 31 def method_missing (method, *args) return super if !@context && global_keys && !global_keys.include?(method) return super if @context && context_keys && !context_keys.include?(method) self.settings[@context] ||= {} self.settings[@context][method] = args[0] if args.size > 0 end |
Instance Attribute Details
#context_keys ⇒ Object
Returns the value of attribute context_keys
2 3 4 |
# File 'lib/appserver/configurator.rb', line 2 def context_keys @context_keys end |
#global_keys ⇒ Object
Returns the value of attribute global_keys
2 3 4 |
# File 'lib/appserver/configurator.rb', line 2 def global_keys @global_keys end |
#settings ⇒ Object
Returns the value of attribute settings
2 3 4 |
# File 'lib/appserver/configurator.rb', line 2 def settings @settings end |
Instance Method Details
#apply!(target, context = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/appserver/configurator.rb', line 11 def apply! (target, context = nil) settings = (self.settings[nil] || {}).dup settings.update(self.settings[context] || {}) if context target.class.const_get(:SETTINGS_DEFAULTS).each do |key, default_value| value = settings[key] || default_value value = File.(value, target.path) if value && target.class.const_get(:SETTINGS_EXPAND).include?(key) target.send("#{key}=", value) end end |
#context(context) ⇒ Object Also known as: app
21 22 23 24 25 26 |
# File 'lib/appserver/configurator.rb', line 21 def context (context) saved_context = @context @context = context.to_s yield @context = saved_context end |