From 6089d570b6c4888923b9d2a56b533ab2e31d9223 Mon Sep 17 00:00:00 2001 From: Dave Gynn Date: Wed, 31 Aug 2016 11:26:24 -0700 Subject: [PATCH] eliminate Array extensions This changes the modifications of the Array class to instead directly extend the Array instance when awesome_method support is needed. --- lib/awesome_print.rb | 2 +- .../{array.rb => awesome_method_array.rb} | 27 +++++++------------ lib/awesome_print/core_ext/class.rb | 1 + lib/awesome_print/core_ext/object.rb | 1 + 4 files changed, 12 insertions(+), 19 deletions(-) rename lib/awesome_print/core_ext/{array.rb => awesome_method_array.rb} (75%) diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index d769ac1..06358a6 100644 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -8,7 +8,7 @@ # so do nothing for subsequent requires. # unless defined?(AwesomePrint::Inspector) - %w(array string method object class kernel).each do |file| + %w(awesome_method_array string method object class kernel).each do |file| require "awesome_print/core_ext/#{file}" end diff --git a/lib/awesome_print/core_ext/array.rb b/lib/awesome_print/core_ext/awesome_method_array.rb similarity index 75% rename from lib/awesome_print/core_ext/array.rb rename to lib/awesome_print/core_ext/awesome_method_array.rb index 54f7a68..b05dbf3 100644 --- a/lib/awesome_print/core_ext/array.rb +++ b/lib/awesome_print/core_ext/awesome_method_array.rb @@ -12,31 +12,24 @@ # # If you could think of a better way please let me know :-) # -class Array #:nodoc: +module AwesomeMethodArray #:nodoc: - alias :difference_without_awesome_print :- - def -(other_ary) - arr = difference_without_awesome_print(other_ary) - if self.instance_variable_defined?(:@__awesome_methods__) + def -(_other_ary) + super.tap do |arr| arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) end - arr end - alias :intersection_without_awesome_print :& - def &(other_ary) - arr = intersection_without_awesome_print(other_ary) - if self.instance_variable_defined?(:@__awesome_methods__) + def &(_other_ary) + super.tap do |arr| arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) end - arr end # # Intercepting Array#grep needs a special treatment since grep accepts # an optional block. # - alias :original_grep :grep def grep(pattern, &blk) # # The following looks rather insane and I've sent numerous hours trying @@ -62,9 +55,9 @@ class Array #:nodoc: # the comment :-) # arr = unless blk - original_grep(pattern) + super(pattern) else - original_grep(pattern) do |match| + super(pattern) do |match| # # The binding can only be used with Ruby-defined methods, therefore # we must rescue potential "ArgumentError: Can't create Binding from @@ -79,10 +72,8 @@ class Array #:nodoc: yield match end end - if self.instance_variable_defined?(:@__awesome_methods__) - arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) - arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap. - end + arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__)) + arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap. arr end end diff --git a/lib/awesome_print/core_ext/class.rb b/lib/awesome_print/core_ext/class.rb index f9b2743..7747b5a 100644 --- a/lib/awesome_print/core_ext/class.rb +++ b/lib/awesome_print/core_ext/class.rb @@ -16,6 +16,7 @@ class Class #:nodoc: define_method name do |*args| methods = original_method.bind(self).call(*args) methods.instance_variable_set(:@__awesome_methods__, self) + methods.extend(AwesomeMethodArray) methods end end diff --git a/lib/awesome_print/core_ext/object.rb b/lib/awesome_print/core_ext/object.rb index bdc1655..f3685bf 100644 --- a/lib/awesome_print/core_ext/object.rb +++ b/lib/awesome_print/core_ext/object.rb @@ -16,6 +16,7 @@ class Object #:nodoc: define_method name do |*args| methods = original_method.bind(self).call(*args) methods.instance_variable_set(:@__awesome_methods__, self) + methods.extend(AwesomeMethodArray) methods end end