Class: Lumberjack::Formatter::MultiplyFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/formatter/multiply_formatter.rb

Overview

This formatter can be used to multiply a numeric value by a specified multiplier and optionally round to a specified number of decimal places.

This is useful for unit conversions (e.g., converting seconds to milliseconds) or scaling values for display purposes. Non-numeric values are passed through unchanged.

Instance Method Summary collapse

Constructor Details

#initialize(multiplier, decimals = nil) ⇒ MultiplyFormatter

Returns a new instance of MultiplyFormatter.

Parameters:

  • multiplier (Numeric)

    The multiplier to apply to the value.

  • decimals (Integer, nil) (defaults to: nil)

    The number of decimal places to round the result to. If nil, no rounding is applied.



16
17
18
19
# File 'lib/lumberjack/formatter/multiply_formatter.rb', line 16

def initialize(multiplier, decimals = nil)
  @multiplier = multiplier
  @decimals = decimals
end

Instance Method Details

#call(value) ⇒ Numeric, Object

Multiply a numeric value by the configured multiplier and optionally round.

Parameters:

  • value (Object)

    The value to format. Only numeric values are processed.

Returns:

  • (Numeric, Object)

    The multiplied (and optionally rounded) value if numeric, otherwise returns the original value unchanged.



26
27
28
29
30
31
32
# File 'lib/lumberjack/formatter/multiply_formatter.rb', line 26

def call(value)
  return value unless value.is_a?(Numeric)

  value *= @multiplier
  value = value.round(@decimals) if @decimals
  value
end