class Hanami::Helpers::NumberFormattingHelper::Formatter

Formatter

@since 0.2.0 @api private

Constants

DEFAULT_DELIMITER

Default delimiter

@return [String] default delimiter

@since 0.2.0 @api private

DEFAULT_PRECISION

Default precision

@return [Integer] default precision

@since 0.2.0 @api private

DEFAULT_SEPARATOR

Default separator

@return [String] default separator

@since 0.2.0 @api private

DELIMITATION_REGEX

Regex to delimitate integer part of a number

@return [Regexp] the delimitation regex

@since 0.2.0 @api private

@see Hanami::Helpers::NumberFormatter::Formatter#delimitate

INTEGER_REGEXP

Regex to guess if the number is a integer

@return [Regexp] the guessing regex

@since 0.2.0 @api private

@see Hanami::Helpers::NumberFormatter::Formatter#to_number

Public Class Methods

new(number, options) click to toggle source

Initialize a new formatter

@param number [Numeric,String] the number to format @param options [Hash] options for number formatting @option options [String] :delimiter hundred delimiter @option options [String] :separator fractional part delimiter @option options [Integer] :precision rounding precision

@since 0.2.0 @api private

@see Hanami::Helpers::NumberFormatter::Formatter::DEFAULT_DELIMITER @see Hanami::Helpers::NumberFormatter::Formatter::DEFAULT_SEPARATOR @see Hanami::Helpers::NumberFormatter::Formatter::DEFAULT_PRECISION

# 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

Public Instance Methods

format() click to toggle source

Format number according to the specified options

@return [String] formatted number

@raise [TypeError] if number can't be formatted

@since 0.2.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 148
def format
  parts.join(@separator)
end

Private Instance Methods

delimitate(part) click to toggle source

Delimitate the given part

@return [String] delimitated string

@since 0.2.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 171
def delimitate(part)
  part.gsub(DELIMITATION_REGEX) { |digit| "#{digit}#{@delimiter}" }
end
parts() click to toggle source

Return integer and fractional parts

@return [Array] parts

@since 0.2.0 @api private

# 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
precision() click to toggle source

Returns precision with a fallback to default value

@return [Numeric] precision

@since 1.0.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 217
def precision
  @precision || DEFAULT_PRECISION
end
precision_requested_explicitly?() click to toggle source

Checks if precision was requested in options

@return [TrueClass,FalseClass] the result of the check

@since 1.0.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 227
def precision_requested_explicitly?
  !@precision.nil?
end
rounded_number() click to toggle source

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

@return [Float,Complex,Rational,BigDecimal] rounded number, if applicable

@since 0.2.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 238
def rounded_number
  if @number.respond_to?(:round)
    @number.round(precision)
  else
    @number
  end
end
to_number() click to toggle source

Numeric coercion

@return [Numeric] coerced number

@raise [TypeError] if number can't be formatted

@since 0.2.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 200
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_str() click to toggle source

String coercion

@return [String] coerced number

@raise [TypeError] if number can't be formatted

@since 0.2.0 @api private

# File lib/hanami/helpers/number_formatting_helper.rb, line 183
def to_str
  number = to_number
  if precision_requested_explicitly?
    Kernel.format("%.#{precision}f", number)
  else
    number.to_s
  end
end