lib/cldr-plurals/ruby_runtime.rb in cldr-plurals-runtime-rb-1.0.1 vs lib/cldr-plurals/ruby_runtime.rb in cldr-plurals-runtime-rb-1.1.0

- old
+ new

@@ -1,81 +1,61 @@ # encoding: UTF-8 +require 'cldr-plurals/ruby-runtime/str_num' + module CldrPlurals module RubyRuntime class << self def build_args_for(num_str) + num = StrNum.from_string(num_str) + [ - n(num_str), i(num_str), f(num_str), - t(num_str), v(num_str), w(num_str) + n(num), i(num), f(num), + t(num), v(num), w(num), + e(num) ] end - def n(str) - to_num( - if str.include?('.') - _n(str).gsub(/([0]+\z)/, '').chomp('.') - else - _n(str) - end - ) - end - - def i(str) - to_num(_i(str)) - end - - def f(str) - to_num(_f(str)) - end - - def t(str) - to_num(_t(str)) - end - - def v(str) - to_num(_v(str)) - end - - def w(str) - to_num(_w(str)) - end - - private - - def to_num(str) - str.include?('.') ? str.to_f : str.to_i - end - # absolute value of the source number (integer and decimals). - def _n(str) - str.gsub(/(-)(.*)/, '\2') + def n(num) + wrap(num).abs.strip.to_val end # integer digits of n. - def _i(str) - _n(str).gsub(/([\d]+)(\..*)/, '\1') + def i(num) + wrap(num).apply_exp.int_val end # visible fractional digits in n, with trailing zeros. - def _f(str) - _n(str).gsub(/([\d]+\.?)(.*)/, '\2') + def f(num) + wrap(num).apply_exp.frac_val.to_i end # visible fractional digits in n, without trailing zeros. - def _t(str) - _f(str).gsub(/([0]+\z)/, '') + def t(num) + wrap(num).apply_exp.strip.frac_val.to_i end # number of visible fraction digits in n, with trailing zeros. - def _v(str) - _f(str).length.to_s + def v(num) + wrap(num).apply_exp.frac.length end # number of visible fraction digits in n, without trailing zeros. - def _w(str) - _t(str).length.to_s + def w(num) + wrap(num).apply_exp.strip.frac_val.length + end + + def e(num) + wrap(num).exp + end + + private + + def wrap(str_or_num) + return str_or_num if str_or_num.is_a?(StrNum) + StrNum.from_string(str_or_num) end end end end