class Slayer::Command

Attributes

result[RW]

Public Class Methods

call(*args, &block) click to toggle source
# File lib/slayer/command.rb, line 7
def call(*args, &block)
  execute_call(block, *args) { |c, *a| c.run(*a) }
end
call!(*args, &block) click to toggle source
# File lib/slayer/command.rb, line 11
def call!(*args, &block)
  execute_call(block, *args) { |c, *a| c.run!(*a) }
end

Private Class Methods

execute_call(command_block, *args) { |command, *args| ... } click to toggle source
# File lib/slayer/command.rb, line 17
def execute_call(command_block, *args)
  # Run the Command and capture the result
  command = self.new
  result  = command.tap { yield(command, *args) }.result

  # Throw an exception if we don't return a result
  raise CommandNotImplementedError unless result.is_a? Result

  # Run the command block if one was provided
  unless command_block.nil?
    matcher = Slayer::ResultMatcher.new(result, command)

    command_block.call(matcher)

    # raise error if not all defaults were handled
    unless matcher.handled_defaults?
      raise(CommandResultNotHandledError, 'The pass or fail condition of a result was not handled')
    end

    begin
      matcher.execute_matching_block
    ensure
      matcher.execute_ensure_block
    end
  end

  return result
end

Public Instance Methods

call() click to toggle source

Call the command

# File lib/slayer/command.rb, line 71
def call
  raise NotImplementedError, 'Commands must define method `#call`.'
end
fail!(value: nil, status: :default, message: nil) click to toggle source

Fail the Command

# File lib/slayer/command.rb, line 60
def fail!(value: nil, status: :default, message: nil)
  @result = Result.new(value, status, message)
  @result.fail!
end
pass!(value: nil, status: :default, message: nil) click to toggle source

Pass the Command

# File lib/slayer/command.rb, line 66
def pass!(value: nil, status: :default, message: nil)
  @result = Result.new(value, status, message)
end
run(*args) click to toggle source
# File lib/slayer/command.rb, line 47
def run(*args)
  call(*args)
rescue CommandFailureError
  # Swallow the Command Failure
end
run!(*args) click to toggle source

Run the Command

# File lib/slayer/command.rb, line 54
def run!(*args)
  call(*args)
end