Class: Mongrel2::Config::DSL::Adapter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(targetclass, opts = {}) ⇒ 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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongrel2/config/dsl.rb', line 24

def initialize( targetclass, opts={} )
	self.log.debug "Wrapping a %p" % [ targetclass ]
	@targetclass = targetclass

	# Use the first pair as the primary key
	unless opts.empty?
		first_pair = Hash[ *opts.first ]
		@targetclass.filter( first_pair ).destroy
	end

	@target = @targetclass.new( opts )
	self.decorate_with_column_declaratives( @target )
	self.decorate_with_custom_declaratives( @targetclass )
end

Instance Attribute Details

#targetObject (readonly)

The decorated object



45
46
47
# File 'lib/mongrel2/config/dsl.rb', line 45

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongrel2/config/dsl.rb', line 56

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.



80
81
82
83
# File 'lib/mongrel2/config/dsl.rb', line 80

def decorate_with_custom_declaratives( objectclass )
	return unless objectclass.const_defined?( :DSLMethods )
	self.singleton_class.send( :include, objectclass.const_get(:DSLMethods) )
end

#singleton_classObject



50
51
52
# File 'lib/mongrel2/config/dsl.rb', line 50

def singleton_class
	class << self; self; end
end