Class: Mongrel2::Config::DSL::Adapter
- Inherits:
-
Object
- Object
- Mongrel2::Config::DSL::Adapter
- Extended by:
- Loggability
- Defined in:
- lib/mongrel2/config/dsl.rb
Overview
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.
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
The decorated object.
Instance Method Summary collapse
-
#decorate_with_column_declaratives(adapted_object) ⇒ Object
Add a declarative singleton method for the columns of the
adapted_object
. -
#decorate_with_custom_declaratives(objectclass) ⇒ Object
Mix in methods defined by the “DSLMethods” mixin defined by the class of the object being adapted.
-
#initialize(targetclass, opts = {}, &block) ⇒ Adapter
constructor
Create an instance of the specified
targetclass
using the specifiedopts
as initial values. - #singleton_class ⇒ Object
Constructor Details
#initialize(targetclass, opts = {}, &block) ⇒ Adapter
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.
25 26 27 28 29 30 31 32 |
# File 'lib/mongrel2/config/dsl.rb', line 25 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 |
Instance Attribute Details
#target ⇒ Object (readonly)
The decorated object
40 41 42 |
# File 'lib/mongrel2/config/dsl.rb', line 40 def target @target end |
Instance Method Details
#decorate_with_column_declaratives(adapted_object) ⇒ Object
Add a declarative singleton method for the columns of the adapted_object
.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mongrel2/config/dsl.rb', line 51 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) ⇒ Object
Mix in methods defined by the “DSLMethods” mixin defined by the class of the object being adapted.
75 76 77 78 |
# File 'lib/mongrel2/config/dsl.rb', line 75 def decorate_with_custom_declaratives( objectclass ) return unless objectclass.const_defined?( :DSLMethods ) self.singleton_class.send( :include, objectclass.const_get(:DSLMethods) ) end |
#singleton_class ⇒ Object
45 46 47 |
# File 'lib/mongrel2/config/dsl.rb', line 45 def singleton_class class << self; self; end end |