Class: Functor

Inherits:
Object show all
Defined in:
lib/functor.rb

Defined Under Namespace

Modules: Method

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Functor

Returns a new instance of Functor.



30
# File 'lib/functor.rb', line 30

def initialize( &block ) ; @patterns = []; instance_eval(&block) if block_given? ; end

Instance Method Details

#bind(object) ⇒ Object



34
# File 'lib/functor.rb', line 34

def bind( object ) ; @object = object ; self ; end

#call(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/functor.rb', line 36

def call( *args, &block )
  args.push( block ) if block_given?
  pattern, action = @patterns.find { |pattern, action| match?( args, pattern ) }
  raise ArgumentError.new( "argument mismatch for argument(s): #{args.inspect}." ) unless action
  @object ? @object.instance_exec( *args, &action ) : action.call( *args )
end

#given(*pattern, &block) ⇒ Object



32
# File 'lib/functor.rb', line 32

def given( *pattern, &block ) ; @patterns.push [ pattern, block ] ; self ; end

#to_procObject



43
44
45
# File 'lib/functor.rb', line 43

def to_proc
  lambda { |*args| self.call(*args) }
end