Class: Lanes::Configuration
- Inherits:
-
Object
- Object
- Lanes::Configuration
- Includes:
- Lanes::Concerns::AttrAccessorWithDefault
- Defined in:
- lib/lanes/configuration.rb
Direct Known Subclasses
Class Method Summary collapse
-
.config_option(name, default, options = {}) ⇒ Object
Since changing a config value inadvertently can have pretty drastic consequences that might not be discovered immediately, we log each time a value is changed.
Instance Method Summary collapse
- #get(config, &block) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #on_change(config, &block) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
26 27 28 |
# File 'lib/lanes/configuration.rb', line 26 def initialize @observers=Hash.new{ |hash,key| hash[key] = Array.new } end |
Class Method Details
.config_option(name, default, options = {}) ⇒ Object
Since changing a config value inadvertently can have pretty drastic consequences that might not be discovered immediately, we log each time a value is changed
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lanes/configuration.rb', line 12 def self.config_option( name, default, ={} ) define_method( "#{name}=" ) do | value | old_value = self.send( name ) if old_value && Lanes.logger && ![:silent] Lanes.logger.info "Config option #{name} changed from '#{old_value.inspect}' to '#{value.inspect}'" end instance_variable_set( "@#{name}", value ) if @observers.has_key?(name) @observers[name].each{ |cb| cb.call(value, old_value) } end end attr_reader_with_default(name, default) end |
Instance Method Details
#get(config, &block) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/lanes/configuration.rb', line 34 def get(config, &block) value = self.send(config.to_sym) if block on_change(config,&block) block.call( value, value ) end value end |
#on_change(config, &block) ⇒ Object
30 31 32 |
# File 'lib/lanes/configuration.rb', line 30 def on_change(config, &block) @observers[config.to_sym] << block end |