Class: Kleisli::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/kleisli/run.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_binding) ⇒ Runner

Returns a new instance of Runner.



18
19
20
21
22
23
# File 'lib/kleisli/run.rb', line 18

def initialize(block_binding)
  @hash = {}
  @stopped = false
  @start = false
  @o_self = block_binding.eval('self')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kleisli/run.rb', line 41

def method_missing(m, *args)
  case
  when @o_self.respond_to?(m)
    @o_self.send(m, *args)
  when args.first.kind_of?(Hash) && args.first[:from] && args.size == 1
    extract(m, args.first[:from])
  when args.empty?
    get_value(m)
  when m.to_s =~ /\=$/
    set_value(m, args.first)
  else
    @o_self.send(m, *args)
  end
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



16
17
18
# File 'lib/kleisli/run.rb', line 16

def start
  @start
end

Instance Method Details

#extract(val, m) ⇒ Object

Raises:



25
26
27
28
29
30
31
# File 'lib/kleisli/run.rb', line 25

def extract(val, m)
  called = false
  @start = m unless @start
  m >-> i { called = true; set_value(val, i) }
  raise MonadTerminator.new(m) unless called
  get_value(val)
end

#get_value(k) ⇒ Object



37
38
39
# File 'lib/kleisli/run.rb', line 37

def get_value(k)
  @hash[k]
end

#set_value(k, v) ⇒ Object



33
34
35
# File 'lib/kleisli/run.rb', line 33

def set_value(k, v)
  @hash[k] = v
end