I've got a little DSL that looks like this:
ActivityLogger.log do
activity('27-06-2012') do
eat do |act|
act.duration = 15
act.priority = 5
end
end
end
I want to refactor it so it loses the block params in the innermost block, so it looks like this:
ActivityLogger.log do
activity('27-06-2012') do
eat do
duration = 15
priority = 5
end
end
end
The #eat
method instantiates a Log object:
def eat(&block)
@logs << Log.new(Eat, &block)
end
Log's constructor yields self
in the last line:
def initialize(activity, &block)
@activity = activity
yield self
end
To my mind, that is where the problem is. I've tried both using instance_eval
in the #eat
method (see link#2 below) and removing the yield
statement from the Log's constructor entirely (link#3), but none of these approaches work (the Log object gets created, but doesn't get its #duration
and #priority
methods set).
Here are the links:
1) A working DSL with block parameters
2) Non-working DSL, first refactoring attempt
3) Non-working DSL, second refactoring attempt
Thanks!