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

See Also:

  • Hanami::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

See Also:

  • Hanami::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

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

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

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

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, DEFAULT_PRECISION)
end

Instance Method Details

#delimitate(part) ⇒ String (private)

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.

Delimitate the given part

Since:

  • 0.2.0


171
172
173
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 171

def delimitate(part)
  part.gsub(DELIMITATION_REGEX) { |digit| "#{digit}#{@delimiter}" }
end

#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

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

#partsArray (private)

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.

Return integer and fractional parts

Since:

  • 0.2.0


160
161
162
163
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 160

def parts
  integer_part, fractional_part = to_str.split(DEFAULT_SEPARATOR)
  [delimitate(integer_part), fractional_part].compact
end

#rounded_numberFloat, ... (private)

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.

Round number in case we need to return a Float representation. If @number doesn't respond to #round return the number as it is.

Since:

  • 0.2.0


213
214
215
216
217
218
219
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 213

def rounded_number
  if @number.respond_to?(:round)
    @number.round(@precision)
  else
    @number
  end
end

#to_numberNumeric (private)

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.

Numeric coercion

Raises:

  • (TypeError)

    if number can't be formatted

Since:

  • 0.2.0


195
196
197
198
199
200
201
202
203
204
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 195

def to_number
  case @number
  when NilClass
    raise TypeError
  when ->(n) { n.to_s.match(INTEGER_REGEXP) }
    Utils::Kernel.Integer(@number)
  else
    Utils::Kernel.Float(rounded_number)
  end
end

#to_strString (private)

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.

String coercion

Raises:

  • (TypeError)

    if number can't be formatted

Since:

  • 0.2.0


183
184
185
# File 'lib/hanami/helpers/number_formatting_helper.rb', line 183

def to_str
  to_number.to_s
end