Sha256: 9754e2b785ddb98a0c470a37d63a78836eae745cf79e95982fa1b5df09ff21a3

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

# Functor class provides a way to do psuedo-namspaces
# This is useful for a number of methods.
# This is basically the class in Calibre but is included
# here to remove dependency.

class Functor

  # remove all non-essential methods
  EXCLUDE = /^(__|instance|null$|inspect$|dup$)/
  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(*obj, &func)
    @obj = obj
    @func = func
  end

  def method_missing(op, *args)
    if @obj.empty?
      @func.call(op, *args)
    else
      @func.call(op, *(@obj + args))
    end
  end

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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/functor.rb
facets-1.1.0 lib/facet/functor.rb