Class: ActiveCommand::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/active_command/middleware/command_runner.rb

Defined Under Namespace

Classes: MissingCommandError

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CommandRunner

Returns a new instance of CommandRunner.



6
7
8
# File 'lib/active_command/middleware/command_runner.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_command/middleware/command_runner.rb', line 10

def call(env)

  at = env.fetch(:at, :later)
  cmd = env.fetch(:command, nil)

  result = CommandResult.new(cmd).tap do |cr|
    begin
      raise MissingCommandError if cr.command.nil?
      cr.command.run if at == :later
      cr.command.perform_now if at == :now
    rescue StandardError => e
      cr.error = e
    end
  end

  env[:command_result] = result

  @app.call(env) if @app
end