-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break out array extensions into a separate require
- Loading branch information
Jacob Rothstein
committed
Nov 1, 2009
1 parent
09c6224
commit 5339b54
Showing
2 changed files
with
31 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,22 @@ | ||
module Enumerable | ||
def proxy(method) Proxy.new method, self end | ||
module EnumerableProxy | ||
def self.included(klass) | ||
klass.class_eval do | ||
def proxy(method) Proxy.new method, self end | ||
alias_method :p, :proxy | ||
end | ||
end | ||
|
||
class Proxy | ||
instance_methods.each { |m| undef_method m unless m =~ /^__/ } | ||
|
||
def initialize(method, enumerable) | ||
@method, @enumerable = method, enumerable | ||
end | ||
|
||
def method_missing(method, *args) | ||
@enumerable.send(@method) {|a| a.send method, *args} | ||
end | ||
end | ||
end | ||
|
||
class Array | ||
%w(each map select reject all?).each do |method| | ||
aliased_target, punctuation = method.to_s.sub(/([?!])$/, ''), $1 | ||
with_proxy = "#{aliased_target}_with_proxy#{punctuation}" | ||
without_proxy = with_proxy.sub "with", "without" | ||
|
||
class_eval %{ | ||
def #{with_proxy}(&blk) | ||
blk.nil? ? proxy(:#{method}) : #{without_proxy}(&blk) | ||
end | ||
alias_method :#{without_proxy}, :#{method} | ||
alias_method :#{method}, :#{with_proxy} | ||
} | ||
end | ||
end | ||
Enumerable.instance_eval {include EnumerableProxy} |
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,21 @@ | ||
module EnumerableProxy | ||
module ArrayExtensions | ||
def self.included(klass) | ||
%w(each map select reject all?).each do |method| | ||
aliased_target, punctuation = method.to_s.sub(/([?!])$/, ''), $1 | ||
with_proxy = "#{aliased_target}_with_proxy#{punctuation}" | ||
without_proxy = with_proxy.sub "with", "without" | ||
|
||
klass.class_eval %{ | ||
def #{with_proxy}(&blk) | ||
blk.nil? ? proxy(:#{method}) : #{without_proxy}(&blk) | ||
end | ||
alias_method :#{without_proxy}, :#{method} | ||
alias_method :#{method}, :#{with_proxy} | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
||
Array.instance_eval { include EnumerableProxy::ArrayExtensions } |