Class: Hanami::View::Helpers::NumberFormattingHelper::Formatter Private
- 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
Class Method Summary collapse
- .call(number, delimiter:, separator:, precision:) ⇒ Object private
-
.coerce(number) ⇒ Object
private
Coerces the given number or string into a number.
-
.delimit_integer(integer_part, delimiter) ⇒ String
private
Delimits the given integer part of a number.
-
.parts(string, delimiter) ⇒ Object
private
Returns the integer and fractional parts of the given number string.
-
.to_str(number, precision) ⇒ Object
private
Formats the given number as a string.
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.
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.
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.
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.
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 |