Skip to content

Commit

Permalink
break out array extensions into a separate require
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rothstein committed Nov 1, 2009
1 parent 09c6224 commit 5339b54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
29 changes: 10 additions & 19 deletions lib/enumerable_proxy.rb
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}
21 changes: 21 additions & 0 deletions lib/enumerable_proxy/array_extensions.rb
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 }

0 comments on commit 5339b54

Please sign in to comment.