Module: EasyCommand

Includes:
LegacyErrorHandling
Defined in:
lib/easy_command/ruby-2-specific.rb,
lib/easy_command.rb,
lib/easy_command/errors.rb,
lib/easy_command/result.rb,
lib/easy_command/version.rb,
lib/easy_command/chainable.rb,
lib/easy_command/ruby-3-specific.rb,
lib/easy_command/ruby-2-7-specific.rb,
lib/easy_command/spec_helpers/command_matchers.rb,
lib/easy_command/spec_helpers/mock_command_helper.rb

Overview

it { expect(Extinguishers::PayloadValidator).to have_been_called_with_acp(payload) } it { is_expected.to be_failure } it { is_expected.to have_failed } it { is_expected.to have_failed.with_error(:date, :invalid) } it { is_expected.to have_failed.with_error(:date, :invalid, “The format must be iso8601”) } it { is_expected.to have_error(:date, :invalid) } it { is_expected.to have_error(:date, :invalid, “The format must be iso8601”) }

Defined Under Namespace

Modules: Chainable, ClassMethods, LegacyErrorHandling, SpecHelpers Classes: CommandError, Errors, ExitError, Failure, NotImplementedError, Params, Result, Success

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LegacyErrorHandling

errors_legacy_alias

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



39
40
41
# File 'lib/easy_command.rb', line 39

def result
  @result
end

Class Method Details

.prepended(base) ⇒ Object



35
36
37
# File 'lib/easy_command.rb', line 35

def self.prepended(base)
  base.extend ClassMethods
end

Instance Method Details

#abort(*args, **kwargs) ⇒ Object

Raises:



62
63
64
65
# File 'lib/easy_command.rb', line 62

def abort(*args, result: nil, **kwargs)
  errors.add(*args, **kwargs)
  raise ExitError.new(result: result)
end

#as_sub_commandObject



90
91
92
93
# File 'lib/easy_command.rb', line 90

def as_sub_command
  @as_sub_command = true
  self
end

#assert(*_args) ⇒ Object

Raises:



67
68
69
# File 'lib/easy_command.rb', line 67

def assert(*_args, result: nil)
  raise ExitError.new(result: result) if errors.any?
end

#assert_subObject



49
50
51
52
# File 'lib/easy_command/ruby-2-specific.rb', line 49

def assert_sub(klass, *args)
  warn "/!\\ 'assert_sub' is deprecated, please use 'assert_subcommand' instead."
  assert_subcommand(klass, *args)
end

#assert_subcommand(klass, *args, **kwargs) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
# File 'lib/easy_command/ruby-2-specific.rb', line 40

def assert_subcommand(klass, *args)
  command_instance = klass.new(*args).as_sub_command
  (@sub_commands ||= []) << command_instance
  command = command_instance.call
  return command.result if command.success?
  errors.merge_from(command)
  raise ExitError.new(result: command.result)
end

#callObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/easy_command.rb', line 48

def call
  raise NotImplementedError unless defined?(super)

  result = super
  if errors.none?
    on_success unless @as_sub_command
    Success[result]
  else
    Failure[result].with_errors(errors)
  end
rescue ExitError => e
  Failure[@result || e.result].with_errors(errors)
end

#errorsObject



71
72
73
# File 'lib/easy_command.rb', line 71

def errors
  @_errors ||= EasyCommand::Errors.new(source: self.class)
end

#on_successObject



75
76
77
78
79
# File 'lib/easy_command.rb', line 75

def on_success
  (@sub_commands ||= []).each(&:on_success)

  super if defined?(super)
end