Class: Judges::AsciiLoog

Inherits:
Object show all
Defined in:
lib/judges/ascii_loog.rb

Overview

ASCII wrapper for Loog logging facility.

This class wraps any Loog logger and converts Unicode symbols to ASCII equivalents when the –ascii option is enabled.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Constant Summary collapse

UNICODE_TO_ASCII =

Unicode to ASCII symbol mapping

{
  '👍' => '+',
  '👎' => '-',
  '❌' => '!',
  '👉' => '>',
  '✓' => '+',
  '✗' => '!',
  '►' => '>',
  '◄' => '<',
  '▼' => 'v',
  '▲' => '^'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ AsciiLoog

Initialize the ASCII wrapper.

Parameters:

  • loog (Loog)

    The original logging facility to wrap



33
34
35
# File 'lib/judges/ascii_loog.rb', line 33

def initialize(loog)
  @loog = loog
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Delegate all other methods to the original logger.



73
74
75
# File 'lib/judges/ascii_loog.rb', line 73

def method_missing(method, *, &)
  @loog.send(method, *, &)
end

Instance Method Details

#debug(message) ⇒ Object

Log a debug message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



68
69
70
# File 'lib/judges/ascii_loog.rb', line 68

def debug(message)
  @loog.debug(to_ascii(message))
end

#error(message) ⇒ Object

Log an error message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



62
63
64
# File 'lib/judges/ascii_loog.rb', line 62

def error(message)
  @loog.error(to_ascii(message))
end

#info(message) ⇒ Object

Log an info message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



50
51
52
# File 'lib/judges/ascii_loog.rb', line 50

def info(message)
  @loog.info(to_ascii(message))
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Check if the original logger responds to a method.

Returns:

  • (Boolean)


78
79
80
# File 'lib/judges/ascii_loog.rb', line 78

def respond_to_missing?(method, include_private = false)
  @loog.respond_to?(method, include_private) || super
end

#to_ascii(message) ⇒ String

Convert Unicode symbols to ASCII equivalents.

Parameters:

  • message (String)

    The message to convert

Returns:

  • (String)

    The converted message with ASCII symbols



40
41
42
43
44
45
46
# File 'lib/judges/ascii_loog.rb', line 40

def to_ascii(message)
  result = message.to_s
  UNICODE_TO_ASCII.each do |unicode, ascii|
    result = result.gsub(unicode, ascii)
  end
  result
end

#warn(message) ⇒ Object

Log a warning message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



56
57
58
# File 'lib/judges/ascii_loog.rb', line 56

def warn(message)
  @loog.warn(to_ascii(message))
end