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.



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

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.



6
7
8
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 6

def locale
  @locale
end

#localize_number=(value) ⇒ Object

Sets the attribute localize_number

Parameters:

  • value

    the value to set the attribute localize_number to.



6
7
8
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 6

def localize_number=(value)
  @localize_number = value
end

#localizer_symbolsObject

Returns the value of attribute localizer_symbols.



6
7
8
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 6

def localizer_symbols
  @localizer_symbols
end

#twitter_cldr_readerObject

Returns the value of attribute twitter_cldr_reader.



6
7
8
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 6

def twitter_cldr_reader
  @twitter_cldr_reader
end

Instance Method Details

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



18
19
20
21
22
23
24
# File 'lib/plurimath/formatter/numeric_formatter.rb', line 18

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