Class: TwitterCldr::DataReaders::NumberDataReader
- Inherits:
-
DataReader
- Object
- DataReader
- TwitterCldr::DataReaders::NumberDataReader
- Defined in:
- lib/twitter_cldr/data_readers/number_data_reader.rb
Constant Summary collapse
- DEFAULT_NUMBER_SYSTEM =
"latn"
- ABBREVIATED_MIN_POWER =
3
- ABBREVIATED_MAX_POWER =
14
- NUMBER_MIN =
10 ** ABBREVIATED_MIN_POWER
- NUMBER_MAX =
10 ** (ABBREVIATED_MAX_POWER + 1)
- BASE_PATH =
[:numbers, :formats]
- SYMBOL_PATH =
[:numbers, :symbols]
- TYPE_PATHS =
{ default: [:decimal, :patterns], decimal: [:decimal, :patterns], long_decimal: [:decimal, :patterns, :long], short_decimal: [:decimal, :patterns, :short], currency: [:currency, :patterns], percent: [:percent, :patterns] }
- ABBREVIATED_TYPES =
[:long_decimal, :short_decimal]
- DEFAULT_TYPE =
:decimal
- DEFAULT_FORMAT =
:default
- DEFAULT_SIGN =
:positive
- REDIRECT_REGEX =
/\Anumbers\.formats\.\w+\.patterns\.\w+\z/
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from DataReader
Class Method Summary collapse
Instance Method Summary collapse
- #format_number(number, options = {}) ⇒ Object
- #formatter ⇒ Object
-
#initialize(locale, options = {}) ⇒ NumberDataReader
constructor
A new instance of NumberDataReader.
- #number_system_for(type) ⇒ Object
- #pattern(number = nil, decimal = true) ⇒ Object
- #symbols ⇒ Object
- #tokenizer ⇒ Object
Methods inherited from DataReader
Constructor Details
#initialize(locale, options = {}) ⇒ NumberDataReader
Returns a new instance of NumberDataReader.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 43 def initialize(locale, = {}) super(locale) @type = [:type] || DEFAULT_TYPE unless type && TYPE_PATHS.include?(type.to_sym) raise ArgumentError.new("Type #{type} is not supported") end @format = [:format] || DEFAULT_FORMAT end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
37 38 39 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 37 def format @format end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
37 38 39 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 37 def type @type end |
Class Method Details
.types ⇒ Object
39 40 41 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 39 def self.types TYPE_PATHS.keys end |
Instance Method Details
#format_number(number, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 54 def format_number(number, = {}) precision = [:precision] || 0 pattern_for_number = pattern(number, precision == 0) [:locale] = self.locale if pattern_for_number == '0' # there's no specific formatting for the number in the current locale number.to_s else tokens = tokenizer.tokenize(pattern_for_number) formatter.format(tokens, number, ) end end |
#formatter ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 106 def formatter @formatter ||= begin klass_name = type.to_s.split("_").map(&:capitalize).join klass = TwitterCldr::Formatters.const_get(:"#{klass_name}Formatter") klass.new(self) end end |
#number_system_for(type) ⇒ Object
94 95 96 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 94 def number_system_for(type) (traverse(BASE_PATH + [type]) || {}).fetch(:number_system, DEFAULT_NUMBER_SYSTEM) end |
#pattern(number = nil, decimal = true) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 69 def pattern(number = nil, decimal = true) sign = number < 0 ? :negative : :positive path = BASE_PATH + if valid_type_for?(number, type) TYPE_PATHS[type] else TYPE_PATHS[:default] end pattern = get_pattern_data(path) if pattern[format] pattern = pattern[format] end if number pattern = pattern_for_number(pattern, number, decimal) end if pattern.is_a?(String) pattern_for_sign(pattern, sign) else pattern end end |
#symbols ⇒ Object
98 99 100 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 98 def symbols @symbols ||= traverse(SYMBOL_PATH) end |
#tokenizer ⇒ Object
102 103 104 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 102 def tokenizer @tokenizer ||= TwitterCldr::Tokenizers::NumberTokenizer.new(self) end |