Class: Lotus::Helpers::NumberFormattingHelper::Formatter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/helpers/number_formatting_helper.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Formatter

Since:

  • 0.2.0

Constant Summary collapse

DELIMITATION_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regex to delimitate integer part of a number

Returns:

  • (Regexp)

    the delimitation regex

See Also:

  • Lotus::Helpers::NumberFormatter::Formatter#delimitate

Since:

  • 0.2.0

/(\d)(?=(\d{3})+$)/
INTEGER_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regex to guess if the number is a integer

Returns:

  • (Regexp)

    the guessing regex

See Also:

  • Lotus::Helpers::NumberFormatter::Formatter#to_number

Since:

  • 0.2.0

/\A[\d]+\z/
DEFAULT_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default separator

Returns:

  • (String)

    default separator

Since:

  • 0.2.0

'.'.freeze
DEFAULT_DELIMITER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default delimiter

Returns:

  • (String)

    default delimiter

Since:

  • 0.2.0

','.freeze
DEFAULT_PRECISION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default precision

Returns:

  • (String)

    default precision

Since:

  • 0.2.0

2

Instance Method Summary collapse

Constructor Details

#initialize(number, options) ⇒ Formatter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new formatter

Parameters:

  • number (Numeric, String)

    the number to format

  • options (Hash)

    options for number formatting

Options Hash (options):

  • :delimiter (String)

    hundred delimiter

  • :separator (String)

    fractional part delimiter

  • :precision (Integer)

    rounding precision

See Also:

  • Lotus::Helpers::NumberFormatter::Formatter::DEFAULT_DELIMITER
  • Lotus::Helpers::NumberFormatter::Formatter::DEFAULT_SEPARATOR
  • Lotus::Helpers::NumberFormatter::Formatter::DEFAULT_PRECISION

Since:

  • 0.2.0


130
131
132
133
134
135
# File 'lib/lotus/helpers/number_formatting_helper.rb', line 130

def initialize(number, options)
  @number = number
  @delimiter = options.fetch(:delimiter, DEFAULT_DELIMITER)
  @separator = options.fetch(:separator, DEFAULT_SEPARATOR)
  @precision = options.fetch(:precision, DEFAULT_PRECISION)
end

Instance Method Details

#formatString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Format number according to the specified options

Returns:

  • (String)

    formatted number

Raises:

  • (TypeError)

    if number can't be formatted

Since:

  • 0.2.0


145
146
147
# File 'lib/lotus/helpers/number_formatting_helper.rb', line 145

def format
  parts.join(@separator)
end