SettingInstaller

class
Superclass
Object
Extended With
Loggability

Methods for declaring config methods and constants inside a `configurability` block.

Attributes

target[R]

The target object

Public Class Methods

anchor
new( target )

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

anchor
setting( name, **options, &block )

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

anchor
add_default( name, options )

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
anchor
add_setting_accessors( name, options, &writer_hook )

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
anchor
make_setting_predicate( name, options )

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
anchor
make_setting_reader( name, options )

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
anchor
make_setting_writer( name, options, &writer_hook )

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