Class: Plurimath::Formatter::NumericFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numeric_formatter.rb

Constant Summary collapse

LOCALIZE_NUMBER_REGEX =
%r{(?<group>[^#])?(?<groupdigits>#+0)(?<decimal>.)(?<fractdigits>#+)(?<fractgroup>[^#])?}
SUPPORTED_NOTATIONS =
%i[e scientific engineering].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, localize_number:, localizer_symbols:) ⇒ NumericFormatter

Returns a new instance of NumericFormatter.



16
17
18
19
20
21
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 16

def initialize(locale, localize_number:, localizer_symbols:)
  @locale = locale
  @localize_number = localize_number
  @localizer_symbols = localizer_symbols
  @twitter_cldr_reader = twitter_cldr_reader_lookup
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



11
12
13
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 11

def locale
  @locale
end

#localize_number=(value) ⇒ Object

Sets the attribute localize_number

Parameters:

  • value

    the value to set the attribute localize_number to.



11
12
13
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 11

def localize_number=(value)
  @localize_number = value
end

#localizer_symbolsObject

Returns the value of attribute localizer_symbols.



11
12
13
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 11

def localizer_symbols
  @localizer_symbols
end

#twitter_cldr_readerObject

Returns the value of attribute twitter_cldr_reader.



11
12
13
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 11

def twitter_cldr_reader
  @twitter_cldr_reader
end

Instance Method Details

#localized_number(number_string, locale:, precision:, format:) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 23

def localized_number(number_string, locale:, precision:, format:)
  options_instance_variables(number_string, format, precision)
  @twitter_cldr_reader.merge!(format)
  return send("#{@notation}_format", number_string) if SUPPORTED_NOTATIONS.include?(@notation&.to_sym)

  localize_number(number_string)
end