Configurability::
SettingInstaller
class
Methods for declaring config methods and constants inside a ‘configurability` block.
- target R
The target object
Create a new Generator that can be used to add configuration methods and constants to the specified target
object.
def initialize( target )
@target = target
end
setting( name, **options, &block )
Declare a config setting with the specified name
.
def setting( name, **options, &block )
self.log.debug " adding %s setting to %p" % [ name, self.target ]
options[:alias] = Array( options[:alias] )
self.add_setting_accessors( name, options, &block )
self.add_default( name, options[:default] )
end
setting_default( name, new_default )
Declare a new default for the setting with the specified name
.
def setting_default( name, new_default )
self.add_default( name, new_default )
end
Protected Instance Methods
add_default( name, default_value )
Add a default for name
to the CONFIG_DEFAULTS constant of the target, creating it if necessary.
def add_default( name, default_value )
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 )
Add accessors with the specified name
to the target.
def add_setting_accessors( name, options, &writer_hook )
self.check_for_duplicates( name, options )
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 )
predicate = self.make_setting_predicate( name, options )
names = [ name ] + options[:alias]
names.each do |setting_name|
self.target.define_singleton_method( "#{setting_name}", &reader )
self.target.define_singleton_method( "#{setting_name}=", &writer )
self.target.define_singleton_method( "#{setting_name}?", &predicate ) if
options[:predicate]
end
end
check_for_duplicates( name, options )
Check that there are not already accessors for the setting with the given name
and options
.
def check_for_duplicates( name, options )
names = [ name.to_sym ] + options[:alias]
names.each do |method_name|
raise ScriptError, "setting %p collides with method #%s" % [ name, method_name ] if
self.target.singleton_methods.include?( name )
raise ScriptError, "setting %p collides with method #%s=" % [ name, method_name ] if
self.target.singleton_methods.include?( "#{method_name}=".to_sym )
if options[:predicate]
raise ScriptError, "setting %p collides with method #%s?" % [ name, method_name ] if
self.target.singleton_methods.include?( "#{method_name}?".to_sym )
end
end
end
make_setting_predicate( name, options )
Create the body of the setting predicate method with the specified name
and options
.
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 )
Create the body of the setting reader method with the specified name
and options
.
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 )
Create the body of the setting writer method with the specified name
and options
.
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