Class: Catalyst::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/catalyst/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
# File 'lib/catalyst/runner.rb', line 5

def initialize(actions)
  @history = []
  @actions = actions.map {|act| setup_action(act) }
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



3
4
5
# File 'lib/catalyst/runner.rb', line 3

def actions
  @actions
end

#historyObject (readonly)

Returns the value of attribute history.



3
4
5
# File 'lib/catalyst/runner.rb', line 3

def history
  @history
end

Instance Method Details

#call(env = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/catalyst/runner.rb', line 10

def call(env={})
  return if actions.empty?

  begin
    action = history.unshift(@actions.shift).first
    action.call(env)
  rescue Exception => e
    env["catalyst.error"] = e
    raise
  end
end

#setup_action(action) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/catalyst/runner.rb', line 22

def setup_action(action)
  klass, args, block = action
  
  if klass.is_a?(Class)
    klass.new(*[self, args].flatten.compact, &block)
  elsif klass.respond_to?(:call)
    lambda do |env|
      klass.call(*[env, self].flatten.compact)
    end
  else
    raise
  end
end