Class: Frivol::Functor

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

Overview

Frivol::Functor

Compiles proc, symbols, false, true into a proc that executes within a scope, or on an object.

Instance Method Summary collapse

Constructor Details

#initialize(method, default = nil) ⇒ Functor

Create a new functor which takes: method: can be a proc, symbol, false or true default: the value which is returned from the compiled proc if method is

not a proc, symbol, false or true. Defaults to nil


10
11
12
13
# File 'lib/frivol/functor.rb', line 10

def initialize(method, default=nil)
  @method  = method
  @default = default
end

Instance Method Details

#compileObject

returns a compiled proc based on the initialization arguments



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/frivol/functor.rb', line 16

def compile
  case @method
  when Proc
    method = @method
    proc do |*args|
      args.unshift(self)
      method.call(*args)
    end
  when Symbol
    method = @method
    proc do |*args|
      self.send(method, *args)
    end
  when FalseClass, TrueClass
    proc{ @method }
  else
    default_return = @default
    proc{ default_return }
  end
end