Module: Functor::Method

Defined in:
lib/functor.rb

Class Method Summary collapse

Class Method Details

.copy_functors(functors) ⇒ Object



6
7
8
9
10
11
# File 'lib/functor.rb', line 6

def self.copy_functors( functors )
  r = {} ; functors.each do | name, functor |
    r[ name ] = functor.clone
  end
  return r
end

.included(k) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/functor.rb', line 12

def self.included( k )
  def k.functors
    @__functors ||= superclass.respond_to?( :functors ) ? 
      Functor::Method.copy_functors( superclass.functors ) : {}
  end
  def k.functor( name, *args, &block )
    name = name.to_sym
    ( f = ( functors[ name ] or 
      ( functors[ name ] = Functor.new ) ) ).given( *args, &block )
    define_method( name ) { | *args | instance_exec( *args, &f.match( *args ) ) } 
  end
  def k.functor_with_self( name, *args, &block )
    name = name.to_sym
    ( f = ( functors[ name ] or 
      ( functors[ name ] = Functor.new ) ) ).given( *args, &block )
    define_method( name ) { | *args | instance_exec( *args, &f.match( self, *args ) ) } 
  end
end