From f5b663d163f8527ff598b4a8e2ee40c46933b8ab Mon Sep 17 00:00:00 2001 From: Peter Boling Date: Fri, 6 Jun 2014 02:27:49 -0700 Subject: [PATCH] Latest version of blank slate from Jim https://github.com/jimweirich/builder/blob/master/lib/blankslate.rb --- lib/blankslate.rb | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/blankslate.rb b/lib/blankslate.rb index a90d5f7..931c8a7 100644 --- a/lib/blankslate.rb +++ b/lib/blankslate.rb @@ -8,6 +8,30 @@ # above copyright notice is included. #++ +class String + if instance_methods.first.is_a?(Symbol) + def _blankslate_as_name + to_sym + end + else + def _blankslate_as_name + self + end + end +end + +class Symbol + if instance_methods.first.is_a?(Symbol) + def _blankslate_as_name + self + end + else + def _blankslate_as_name + to_s + end + end +end + ###################################################################### # BlankSlate provides an abstract base class with no predefined # methods (except for \_\_send__ and \_\_id__). @@ -16,16 +40,20 @@ # class BlankSlate class << self - + # Hide the method named +name+ in the BlankSlate class. Don't # hide +instance_eval+ or any method beginning with "__". def hide(name) - if instance_methods.map(&:to_sym).include?(name.to_sym) and - name !~ /^(__|instance_eval|object_id)/ + warn_level = $VERBOSE + $VERBOSE = nil + if instance_methods.include?(name._blankslate_as_name) && + name !~ /^(__|instance_eval$)/ @hidden_methods ||= {} @hidden_methods[name.to_sym] = instance_method(name) undef_method name end + ensure + $VERBOSE = warn_level end def find_hidden_method(name) @@ -41,7 +69,7 @@ def reveal(name) define_method(name, hidden_method) end end - + instance_methods.each { |m| hide(m) } end @@ -106,4 +134,4 @@ def append_features(mod) end result end -end \ No newline at end of file +end