aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-10 23:01:41 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-10 23:01:41 +0000
commite7040b09e8dc381c746a0a3b4dff540eebe7a35d (patch)
tree842c2cfeb39de4cb23d20f910297d6b6da7be221 /activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb
parentcff25aae90859d5f47d827b162b505f8b316b733 (diff)
downloadrails-e7040b09e8dc381c746a0a3b4dff540eebe7a35d.tar.gz
rails-e7040b09e8dc381c746a0a3b4dff540eebe7a35d.tar.bz2
rails-e7040b09e8dc381c746a0a3b4dff540eebe7a35d.zip
Rebundle Builder 2.1.2 but prefer a newer RubyGem if available.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7828 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb')
-rw-r--r--activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb113
1 files changed, 113 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb b/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb
new file mode 100644
index 0000000000..da6034d9c4
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb
@@ -0,0 +1,113 @@
+#!/usr/bin/env ruby
+#--
+# Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
+# All rights reserved.
+
+# Permission is granted for use, copying, modification, distribution,
+# and distribution of modified versions of this work as long as the
+# above copyright notice is included.
+#++
+
+######################################################################
+# BlankSlate provides an abstract base class with no predefined
+# methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
+# BlankSlate is useful as a base class when writing classes that
+# depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
+#
+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.include?(name.to_s) and
+ name !~ /^(__|instance_eval)/
+ @hidden_methods ||= {}
+ @hidden_methods[name.to_sym] = instance_method(name)
+ undef_method name
+ end
+ end
+
+ def find_hidden_method(name)
+ @hidden_methods ||= {}
+ @hidden_methods[name] || superclass.find_hidden_method(name)
+ end
+
+ # Redefine a previously hidden method so that it may be called on a blank
+ # slate object.
+ def reveal(name)
+ bound_method = nil
+ unbound_method = find_hidden_method(name)
+ fail "Don't know how to reveal method '#{name}'" unless unbound_method
+ define_method(name) do |*args|
+ bound_method ||= unbound_method.bind(self)
+ bound_method.call(*args)
+ end
+ end
+ end
+
+ instance_methods.each { |m| hide(m) }
+end
+
+######################################################################
+# Since Ruby is very dynamic, methods added to the ancestors of
+# BlankSlate <em>after BlankSlate is defined</em> will show up in the
+# list of available BlankSlate methods. We handle this by defining a
+# hook in the Object and Kernel classes that will hide any method
+# defined after BlankSlate has been loaded.
+#
+module Kernel
+ class << self
+ alias_method :blank_slate_method_added, :method_added
+
+ # Detect method additions to Kernel and remove them in the
+ # BlankSlate class.
+ def method_added(name)
+ result = blank_slate_method_added(name)
+ return result if self != Kernel
+ BlankSlate.hide(name)
+ result
+ end
+ end
+end
+
+######################################################################
+# Same as above, except in Object.
+#
+class Object
+ class << self
+ alias_method :blank_slate_method_added, :method_added
+
+ # Detect method additions to Object and remove them in the
+ # BlankSlate class.
+ def method_added(name)
+ result = blank_slate_method_added(name)
+ return result if self != Object
+ BlankSlate.hide(name)
+ result
+ end
+
+ def find_hidden_method(name)
+ nil
+ end
+ end
+end
+
+######################################################################
+# Also, modules included into Object need to be scanned and have their
+# instance methods removed from blank slate. In theory, modules
+# included into Kernel would have to be removed as well, but a
+# "feature" of Ruby prevents late includes into modules from being
+# exposed in the first place.
+#
+class Module
+ alias blankslate_original_append_features append_features
+ def append_features(mod)
+ result = blankslate_original_append_features(mod)
+ return result if mod != Object
+ instance_methods.each do |name|
+ BlankSlate.hide(name)
+ end
+ result
+ end
+end \ No newline at end of file