Permalink
Browse files

Added lambda merging to OptionMerger (especially useful with named_sc…

…ope and with_options) [#740 state:committed] (Paweł Kondzior)
  • Loading branch information...
1 parent 8d8195d commit 9eaa0a3449595d07fe2ada5c4c93ec226622147c @dhh dhh committed Nov 15, 2008
Showing with 15 additions and 1 deletion.
  1. +7 −1 activesupport/lib/active_support/option_merger.rb
  2. +8 −0 activesupport/test/option_merger_test.rb
View
8 activesupport/lib/active_support/option_merger.rb
@@ -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
View
8 activesupport/test/option_merger_test.rb
@@ -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

0 comments on commit 9eaa0a3

Please sign in to comment.