Class: Plurimath::NumberFormatter

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

Direct Known Subclasses

Formatter::Standard

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = "en", localize_number: nil, localizer_symbols: {}, precision: nil) ⇒ NumberFormatter

Returns a new instance of NumberFormatter.



7
8
9
10
11
12
# File 'lib/plurimath/number_formatter.rb', line 7

def initialize(locale = "en", localize_number: nil, localizer_symbols: {}, precision: nil)
  @locale = supported_locale(locale)
  @localize_number = localize_number
  @localizer_symbols = localizer_symbols
  @precision = precision
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



5
6
7
# File 'lib/plurimath/number_formatter.rb', line 5

def locale
  @locale
end

#localize_numberObject

Returns the value of attribute localize_number.



5
6
7
# File 'lib/plurimath/number_formatter.rb', line 5

def localize_number
  @localize_number
end

#localizer_symbolsObject

Returns the value of attribute localizer_symbols.



5
6
7
# File 'lib/plurimath/number_formatter.rb', line 5

def localizer_symbols
  @localizer_symbols
end

#precisionObject

Returns the value of attribute precision.



5
6
7
# File 'lib/plurimath/number_formatter.rb', line 5

def precision
  @precision
end

Instance Method Details

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/plurimath/number_formatter.rb', line 14

def localized_number(number_string, locale: @locale, precision: @precision, format: {})
  prev_symbols = symbols(locale).dup
  Formatter::NumericFormatter.new(
    supported_locale(locale),
    localize_number: localize_number,
    localizer_symbols: localizer_symbols,
  ).localized_number(
    number_string,
    locale: supported_locale(locale),
    precision: precision,
    format: format,
  )
ensure
  symbols(locale).replace(prev_symbols)
end

#twitter_cldr_reader(locale: @locale) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/plurimath/number_formatter.rb', line 30

def twitter_cldr_reader(locale: @locale)
  Formatter::NumericFormatter.new(
    supported_locale(locale),
    localize_number: localize_number,
    localizer_symbols: localizer_symbols,
  ).twitter_cldr_reader
end