Class: Lifer::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/lifer/message.rb

Overview

This message class lets us output rich messages to STDOUT without muddying up the Lifer source code with ad hoc ‘puts` statements. Using this interface helps us ensure that translations are accounted for, and it lets us format errors and log messages in a consistent way.

If the program is in test mode, this means the message should not be output to STDOUT.

Constant Summary collapse

ANSI_COLOURS =

ANSI colour codes for colours used by different messages.

{red: "31"}

Class Method Summary collapse

Class Method Details

.error(translation_key, test_mode: test_mode?, , **args) ⇒ void

This method returns an undefined value.

Outputs a red error message into STDOUT for higher visibility. Note that this is still just a message, and the program might not exit due to an exception having been raised.

Parameters:

  • translation_key (String)

    A translation key that can be read by the ‘I18n` library.

  • test_mode (boolean) (defaults to: test_mode?, )

    Whether the message should be output as if the program were in test mode.

  • args (**Hash)

    A catch-all keyword arguments to be passed on to ‘I18n.t!`.



26
27
28
29
30
31
32
33
# File 'lib/lifer/message.rb', line 26

def error(translation_key, test_mode: test_mode?, **args)
  return if test_mode

  prefix = I18n.t("message.prefix.error")
  error_message = I18n.t!(translation_key, **args)

  puts colorize(("%s: %s" % [prefix, error_message]), :red)
end

.log(translation_key, test_mode: test_mode?, , **args) ⇒ void

This method returns an undefined value.

Outputs a log message into STDOUT.

Parameters:

  • translation_key (String)

    A translation key that can be read by the ‘I18n` library.

  • test_mode (boolean) (defaults to: test_mode?, )

    Whether the message should be output as if the program were in test mode.

  • args (**Hash)

    A catch-all keyword arguments to be passed on to ‘I18n.t!`.



44
45
46
47
48
# File 'lib/lifer/message.rb', line 44

def log(translation_key, test_mode: test_mode?, **args)
  return if test_mode

  puts I18n.t!(translation_key, **args)
end