Sha256: 2ad13bce3c4f1a67496b38cf9d688f80c19d4d94b9e029f6ffba903cfdbcd582

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

# MetaFunctor Class provides a way to do psuedo-namspaces
# This is useful for a number of methods, especailly __meta__.
# This is similar to class in Mega but is included here
# to remove dependency.

class MetaFunctor
  # remove all non-essential methods
  EXCLUDE = /^(__|instance|null$|inspect$)/
  class << self
    def hide(name)
      if method_defined?(name) and name !~ EXCLUDE
        undef_method( name )
      end
    end
  end
  instance_methods.each { |m| hide(m) }

  # primary functionality
  def initialize(&func)
    @func = func
  end
  def method_missing(op, *args)
    @func.call(op, *args)
  end

  # prevent new methods from being added
  class << self
    def method_added( name )
      hide( name )
    end
  end
end


class Object

  # Returns a Functor that allows one to call any
  # Kernel method bound to self.
  #
  #   class A
  #     def object_id ; "OBTUSE" ; end
  #   end
  #
  #   c = C.new
  #   c.object_id             #=> "OBTUSE"
  #   c.__meta__.object_id    #=> 6664875832
  #
  def __meta__
    @__meta__ ||= MetaFunctor.new do |meth, *args|  # &blk|
      Kernel.instance_method(meth).bind(self).call(*args) # ,&blk)
    end
  end

  alias_method :meta, :__meta__  # should this even be available?

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/kernel/__meta__.rb