-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Object#with_options for DRYing up multiple calls to methods havin…
…g shared options git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3314 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
1 parent
fc4ffbd
commit 85fe1ec
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module ActiveSupport | ||
class OptionMerger #:nodoc: | ||
instance_methods.each do |method| | ||
undef_method(method) if method !~ /^(__|instance_eval)/ | ||
end | ||
|
||
def initialize(context, options) | ||
@context, @options = context, options | ||
end | ||
|
||
private | ||
def method_missing(method, *arguments, &block) | ||
merge_argument_options! arguments | ||
@context.send(method, *arguments, &block) | ||
end | ||
|
||
def merge_argument_options!(arguments) | ||
arguments << if arguments.last.respond_to? :merge! | ||
arguments.pop.dup.merge!(@options) | ||
else | ||
@options.dup | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'test/unit' | ||
|
||
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' | ||
require 'active_support' | ||
|
||
class OptionMergerTest < Test::Unit::TestCase | ||
def setup | ||
@options = {:hello => 'world'} | ||
end | ||
|
||
def test_method_with_options_merges_options_when_options_are_present | ||
local_options = {:cool => true} | ||
|
||
with_options(@options) do |o| | ||
assert_equal local_options, method_with_options(local_options) | ||
assert_equal @options.merge(local_options), | ||
o.method_with_options(local_options) | ||
end | ||
end | ||
|
||
def test_method_with_options_appends_options_when_options_are_missing | ||
with_options(@options) do |o| | ||
assert_equal Hash.new, method_with_options | ||
assert_equal @options, o.method_with_options | ||
end | ||
end | ||
|
||
private | ||
def method_with_options(options = {}) | ||
options | ||
end | ||
end |