class Configurability::SettingInstaller
Methods for declaring config methods and constants inside a `configurability` block.
Attributes
target[R]
The target object
Public Class Methods
new( target )
click to toggle source
Create a new Generator that can be used to add configuration methods and constants to the specified target
object.
# File lib/configurability/setting_installer.rb, line 17 def initialize( target ) @target = target end
Public Instance Methods
setting( name, **options, &block )
click to toggle source
Declare a config setting with the specified name
.
# File lib/configurability/setting_installer.rb, line 28 def setting( name, **options, &block ) self.log.debug " adding %s setting to %p" % [ name, self.target ] self.add_setting_accessors( name, options, &block ) self.add_default( name, options ) end
Protected Instance Methods
add_default( name, options )
click to toggle source
Add a default for name
to the CONFIG_DEFAULTS constant of the target, creating it if necessary.
# File lib/configurability/setting_installer.rb, line 112 def add_default( name, options ) default_value = options[ :default ] self.target.send( "#{name}=", default_value ) if self.target.respond_to?( :const_defined? ) defaults = if self.target.const_defined?( :CONFIG_DEFAULTS, false ) self.target.const_get( :CONFIG_DEFAULTS, false ) else self.target.const_set( :CONFIG_DEFAULTS, {} ) end defaults.store( name, default_value ) end end
add_setting_accessors( name, options, &writer_hook )
click to toggle source
Add accessors with the specified name
to the target.
# File lib/configurability/setting_installer.rb, line 40 def add_setting_accessors( name, options, &writer_hook ) if options[:use_class_vars] self.target.class_variable_set( "@@#{name}", nil ) else self.target.instance_variable_set( "@#{name}", nil ) end reader = self.make_setting_reader( name, options ) writer = self.make_setting_writer( name, options, &writer_hook ) self.target.define_singleton_method( "#{name}", &reader ) self.target.define_singleton_method( "#{name}=", &writer ) if options[:predicate] predicate = self.make_setting_predicate( name, options ) self.target.define_singleton_method( "#{name}?", &predicate ) end end
make_setting_predicate( name, options )
click to toggle source
Create the body of the setting predicate method with the specified name
and options
.
# File lib/configurability/setting_installer.rb, line 95 def make_setting_predicate( name, options ) if options[:use_class_vars] return lambda do Loggability[ Configurability ].debug "Using class variables for %s of %p" % [ name, self ] self.class_variable_get("@@#{name}") ? true : false end else return lambda { self.instance_variable_get("@#{name}") ? true : false } end end
make_setting_reader( name, options )
click to toggle source
Create the body of the setting reader method with the specified name
and options
.
# File lib/configurability/setting_installer.rb, line 61 def make_setting_reader( name, options ) if options[:use_class_vars] return lambda do Loggability[ Configurability ].debug "Using class variables for %s of %p" % [ name, self ] self.class_variable_get("@@#{name}") end else return lambda { self.instance_variable_get("@#{name}") } end end
make_setting_writer( name, options, &writer_hook )
click to toggle source
Create the body of the setting writer method with the specified name
and options
.
# File lib/configurability/setting_installer.rb, line 77 def make_setting_writer( name, options, &writer_hook ) if options[:use_class_vars] return lambda do |value| Loggability[ Configurability ].debug "Using class variables for %s of %p" % [ name, self ] value = writer_hook[ value ] if writer_hook self.class_variable_set( "@@#{name}", value ) end else return lambda do |value| value = writer_hook[ value ] if writer_hook self.instance_variable_set( "@#{name}", value ) end end end