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

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

  • 2.1.0

Class Method Summary collapse

Class Method Details

.call(number, delimiter:, separator:, precision:) ⇒ Object

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.



115
116
117
118
119
120
121
# File 'lib/hanami/view/helpers/number_formatting_helper.rb', line 115

def self.call(number, delimiter:, separator:, precision:)
  number = coerce(number)
  str = to_str(number, precision)
  array = parts(str, delimiter)

  array.join(separator)
end

.coerce(number) ⇒ Object

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.

Coerces the given number or string into a number.

Since:

  • 2.1.0



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hanami/view/helpers/number_formatting_helper.rb', line 127

def self.coerce(number)
  case number
  when NilClass
    raise ArgumentError, "failed to convert #{number.inspect} to number"
  when ->(n) { n.to_s.match(INTEGER_REGEXP) }
    Integer(number)
  else
    begin
      Float(number)
    rescue TypeError
      raise ArgumentError, "failed to convert #{number.inspect} to float"
    rescue ArgumentError => e
      raise e.class, "failed to convert #{number.inspect} to float"
    end
  end
end

.delimit_integer(integer_part, delimiter) ⇒ String

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.

Delimits the given integer part of a number.

Parameters:

  • integer_part (String)

    integer part of the number

  • delimiter (String)

    hundreds delimiter

Returns:

  • (String)

    delimited integer string

Since:

  • 2.1.0



175
176
177
# File 'lib/hanami/view/helpers/number_formatting_helper.rb', line 175

def self.delimit_integer(integer_part, delimiter)
  integer_part.gsub(DELIMITING_REGEX) { |digit| "#{digit}#{delimiter}" }
end

.parts(string, delimiter) ⇒ Object

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.

Returns the integer and fractional parts of the given number string.

Since:

  • 2.1.0



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

def self.parts(string, delimiter)
  integer_part, fractional_part = string.split(DEFAULT_SEPARATOR)
  [delimit_integer(integer_part, delimiter), fractional_part].compact
end

.to_str(number, precision) ⇒ Object

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.

Formats the given number as a string.

Since:

  • 2.1.0



148
149
150
151
152
153
154
155
# File 'lib/hanami/view/helpers/number_formatting_helper.rb', line 148

def self.to_str(number, precision)
  case number
  when Integer
    number.to_s
  else
    ::Kernel.format("%.#{precision}f", number)
  end
end