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
- PluralRules =
TwitterCldr::Formatters::Plurals::Rules
- DEFAULT_NUMBER_SYSTEM =
:default
- ABBREVIATED_MIN_POWER =
3
- ABBREVIATED_MAX_POWER =
14
- NUMBER_MIN =
10 ** ABBREVIATED_MIN_POWER
- NUMBER_MAX =
10 ** (ABBREVIATED_MAX_POWER + 1)
- PATTERN_PATH =
[:numbers, :formats]
- SYMBOL_PATH =
[:numbers, :symbols]
- TYPES =
[:default, :decimal, :currency, :percent]
- FORMATS =
[:long, :short, :default]
- DEFAULT_TYPE =
:decimal
- DEFAULT_FORMAT =
:default
- DEFAULT_SIGN =
:positive
- FORMATTERS =
{ decimal: TwitterCldr::Formatters::DecimalFormatter, currency: TwitterCldr::Formatters::CurrencyFormatter, percent: TwitterCldr::Formatters::PercentFormatter }
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#number_system ⇒ Object
readonly
Returns the value of attribute number_system.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from DataReader
Class Method Summary collapse
Instance Method Summary collapse
- #default_number_system ⇒ Object
- #format_number(number, options = {}) ⇒ Object
- #formatter ⇒ Object
-
#initialize(locale, options = {}) ⇒ NumberDataReader
constructor
A new instance of NumberDataReader.
- #pattern(number, decimal = true) ⇒ Object
- #pattern_for_sign(pattern, sign) ⇒ Object
- #symbols ⇒ Object
- #tokenizer ⇒ Object
Methods inherited from DataReader
Constructor Details
#initialize(locale, options = {}) ⇒ NumberDataReader
Returns a new instance of NumberDataReader.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 41 def initialize(locale, = {}) super(locale) @type = [:type] || DEFAULT_TYPE unless type && TYPES.include?(type.to_sym) raise ArgumentError.new("Type #{type} is not supported") end @format = [:format] || DEFAULT_FORMAT @number_system = [:number_system] || default_number_system end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
35 36 37 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 35 def format @format end |
#number_system ⇒ Object (readonly)
Returns the value of attribute number_system.
35 36 37 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 35 def number_system @number_system end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
35 36 37 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 35 def type @type end |
Class Method Details
.types ⇒ Object
37 38 39 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 37 def self.types TYPES end |
Instance Method Details
#default_number_system ⇒ Object
95 96 97 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 95 def default_number_system @default_number_system ||= resource[:numbers][:default_number_systems][:default].to_sym end |
#format_number(number, options = {}) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 53 def format_number(number, = {}) precision = [:precision] || 0 pattern_for_number = pattern(number, precision == 0) [:locale] = self.locale tokens = tokenizer.tokenize(pattern_for_number) formatter.format(tokens, number, ) end |
#formatter ⇒ Object
91 92 93 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 91 def formatter @formatter ||= FORMATTERS[type].new(self) end |
#pattern(number, decimal = true) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 61 def pattern(number, decimal = true) zeroes = number.to_i.abs.to_s.size - 1 magnitude = "1#{'0' * zeroes}" truncated_num = formatter.truncate_number(number, zeroes % 3 + 1) truncated_num = truncated_num.to_i if decimal plural_rule = PluralRules.rule_for(truncated_num, locale) path = PATTERN_PATH + [ type, number_system, [format, :default], magnitude.to_sym, [plural_rule, :other] ] sign = number < 0 ? :negative : :positive pattern_for_sign( traverse_finding_best_fit(path, []), sign ) end |
#pattern_for_sign(pattern, sign) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 99 def pattern_for_sign(pattern, sign) if pattern.include?(";") positive, negative = pattern.split(";") sign == :positive ? positive : negative else case sign when :negative "#{symbols[:minus_sign] || '-'}#{pattern}" else pattern end end end |
#symbols ⇒ Object
83 84 85 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 83 def symbols @symbols ||= traverse_following_aliases(SYMBOL_PATH + [number_system]) end |
#tokenizer ⇒ Object
87 88 89 |
# File 'lib/twitter_cldr/data_readers/number_data_reader.rb', line 87 def tokenizer @tokenizer ||= TwitterCldr::Tokenizers::NumberTokenizer.new(self) end |