class Mongrel2::Config::DSL::Adapter

A decorator object that provides the DSL-ish interface to the various Config objects. It derives its interface on the fly from columns of the class it's created with and a DSLMethods mixin if the target class defines one.

Attributes

target[R]

The decorated object

Public Class Methods

new( targetclass, opts={}, &block ) click to toggle source

Create an instance of the specified targetclass using the specified opts as initial values. The first pair of opts will be used in the filter to find any previous instance and delete it.

# File lib/mongrel2/config/dsl.rb, line 24
def initialize( targetclass, opts={}, &block )
        self.log.debug "Wrapping a %p" % [ targetclass ]
        @targetclass = targetclass

        @target = @targetclass.find_or_new( opts, &block )
        self.decorate_with_column_declaratives( @target )
        self.decorate_with_custom_declaratives( @targetclass )
end

Public Instance Methods

decorate_with_column_declaratives( adapted_object ) click to toggle source

Add a declarative singleton method for the columns of the adapted_object.

# File lib/mongrel2/config/dsl.rb, line 50
def decorate_with_column_declaratives( adapted_object )
        columns = adapted_object.columns
        self.log.debug "  decorating for columns: %s" % [ columns.map( &:to_s ).sort.join(', ') ]

        columns.each do |colname|

                # Create a method that will act as a writer if called with an
                # argument, and a reader if not.
                method_body = Proc.new do |*args|
                        if args.empty?
                                self.target.send( colname )
                        else
                                self.target.send( "#{colname}=", *args )
                        end
                end

                # Install the method
                self.singleton_class.send( :define_method, colname, &method_body )
        end
end
decorate_with_custom_declaratives( objectclass ) click to toggle source

Mix in methods defined by the “DSLMethods” mixin defined by the class of the object being adapted.

# File lib/mongrel2/config/dsl.rb, line 74
def decorate_with_custom_declaratives( objectclass )
        return unless objectclass.const_defined?( :DSLMethods )
        self.singleton_class.send( :include, objectclass.const_get(:DSLMethods) )
end
singleton_class() click to toggle source
# File lib/mongrel2/config/dsl.rb, line 44
def singleton_class
        class << self; self; end
end