Class: Hanami::Helpers::NumberFormattingHelper::Formatter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/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:

  • Hanami::Helpers::NumberFormatter::Formatter#delimitate

Since:

  • 0.2.0

/(\d)(?=(\d{3})+$)/.freeze
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:

  • Hanami::Helpers::NumberFormatter::Formatter#to_number

Since:

  • 0.2.0

/\A[\d]+\z/.freeze
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:

  • (Integer)

    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:

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

Since:

  • 0.2.0



133
134
135
136
137
138
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 133

def initialize(number, options)
  @number = number
  @delimiter = options.fetch(:delimiter, DEFAULT_DELIMITER)
  @separator = options.fetch(:separator, DEFAULT_SEPARATOR)
  @precision = options.fetch(:precision, nil)
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



148
149
150
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 148

def format
  parts.join(@separator)
end