Sha256: 9be4ec5f4b901a1b107082b5e5cf61a7cb27ab522797dce9e12f75d3ab38148b

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

#!/usr/bin/env ruby
#--
# Copyright 2004 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.
#++

unless defined? BlankSlate

  # 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)
        undef_method name if
        instance_methods.include?(name.to_s) and
        name !~ /^(__|instance_eval)/
      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 defined
  module Kernel #:nodoc:
    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)
        blank_slate_method_added(name)
        return if self != Kernel
        BlankSlate.hide(name)
      end
    end
  end

  class Object #:nodoc:
    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)
        blank_slate_method_added(name)
        return if self != Object
        BlankSlate.hide(name)
      end
    end
  end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.6.0 lib/more/facets/blankslate.rb
facets-2.4.4 lib/more/facets/blankslate.rb
facets-2.5.0 lib/more/facets/blankslate.rb
facets-2.4.5 lib/more/facets/blankslate.rb
facets-2.5.1 lib/more/facets/blankslate.rb
facets-2.5.2 lib/more/facets/blankslate.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/more/facets/blankslate.rb