Module: MotherBrain::Mixin::CodedExit::ClassMethods

Defined in:
lib/mb/mixin/coded_exit.rb

Instance Method Summary collapse

Instance Method Details

#exit_code_for(const_name) ⇒ Integer Also known as: exit_status_for

Look up the exit status for motherbrain error matching the given name

Examples:

retrieving the exit status for MB::InvalidConfig

exit_code_for("InvalidConfig") #=> 14

Parameters:

  • const_name (String)

    name of the error constant to lookup

Returns:

  • (Integer)


42
43
44
# File 'lib/mb/mixin/coded_exit.rb', line 42

def exit_code_for(const_name)
  MB.const_get(const_name).exit_code
end

#exit_with(obj) ⇒ Object

Exit the application with the exit status associated with the given motherbrain error

Examples:

exit the application with an exit status for InvalidConfig (14)

exit_with(MB::InvalidConfig)

Parameters:

  • obj (String, #exit_code)

Raises:

  • (SystemExit)


26
27
28
29
30
31
# File 'lib/mb/mixin/coded_exit.rb', line 26

def exit_with(obj)
  err_const = obj.is_a?(String) ? constant_for(obj) : obj
  exit_code = err_const.try(:exit_code) || MBError::DEFAULT_EXIT_CODE

  Kernel.exit(exit_code)
end