Skip to content

Commit

Permalink
Added lambda merging to OptionMerger (especially useful with named_sc…
Browse files Browse the repository at this point in the history
…ope and with_options) [#740 state:committed] (Paweł Kondzior)
  • Loading branch information
dhh committed Nov 15, 2008
1 parent 8d8195d commit 9eaa0a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion activesupport/lib/active_support/option_merger.rb
Expand Up @@ -10,7 +10,13 @@ def initialize(context, options)

private
def method_missing(method, *arguments, &block)
arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
if arguments.last.is_a?(Proc)
proc = arguments.pop
arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
else
arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
end

@context.__send__(method, *arguments, &block)
end
end
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/option_merger_test.rb
Expand Up @@ -64,6 +64,14 @@ def test_nested_method_with_options_containing_hashes_going_deep
end
end
end

def test_nested_method_with_options_using_lamdba
local_lamdba = lambda { { :lambda => true } }
with_options(@options) do |o|
assert_equal @options.merge(local_lamdba.call),
o.method_with_options(local_lamdba).call
end
end

# Needed when counting objects with the ObjectSpace
def test_option_merger_class_method
Expand Down

0 comments on commit 9eaa0a3

Please sign in to comment.