lib/h/h.rb in modalh-1.0.4 vs lib/h/h.rb in modalh-1.0.5

- old
+ new

@@ -35,11 +35,11 @@ options[:blank] || '' end end def number_to(value, options={}) - options = I18n.translate(:'number.format', :locale => options[:locale]).except(:precision).merge(options) + options = number_format_options(options).except(:precision).merge(options) precision = options[:precision] return options[:blank] || '' if value.nil? unless value.kind_of?(String) value = round(value,precision) @@ -49,11 +49,11 @@ inf = options[:inf] || '∞' return value<0 ? "-#{inf}" : inf else value = value.to_i if precision==0 value = value.to_s - value = value[0...-2] if value.ends_with?('.0') + value = value[0...-2] if value.end_with?('.0') end end if options[:delimiter] txt = value.to_s.tr(' ','').tr('.,',options[:separator]+options[:delimiter]).tr(options[:delimiter],'') else @@ -71,11 +71,11 @@ end digit_grouping txt, 3, options[:delimiter], txt.index(/\d/), txt.index(options[:separator]) || txt.size end def number_from(txt, options={}) - options = I18n.translate(:'number.format', :locale => options[:locale]).except(:precision).merge(options) + options = number_format_options(options).except(:precision).merge(options) type = check_type(options[:type] || (options[:precision]==0 ? Integer : Float)) return nil if txt.to_s.strip.empty? || txt==options[:blank] if options[:delimiter] @@ -92,11 +92,11 @@ type.new txt end end def integer_to(value, options={}) - options = I18n.translate(:'number.format', :locale => options[:locale]).merge(options) + options = number_format_options(options).merge(options) if value.nil? options[:blank] || '' else value = value.to_s digit_grouping value, 3, options[:delimiter], value.index(/\d/), value.size @@ -118,11 +118,11 @@ def date_to(value, options={}) I18n.l(value, options) end def date_from(txt, options={}) - options = I18n.translate(:'number.format', :locale => options[:locale]).merge(options) + options = number_format_options(options).merge(options) type = check_type(options[:type] || Date) return nil if txt.to_s.strip.empty? || txt==options[:blank] return txt if txt.respond_to?(:strftime) @@ -153,16 +153,16 @@ def datetime_from(txt, options={}) date_from value, options.reverse_merge(:type=>DateTime) end def logical_to(value, options={}) - options = I18n.translate(:'logical.format', :locale => options[:locale]).merge(options) + options = logical_format_options(options).merge(options) value.nil? ? options[:blank] : (value ? options[:true] : options[:false]) end def logical_from(txt, options={}) - options = I18n.translate(:'logical.format', :locale => options[:locale]).merge(options) + options = logical_format_options(options).merge(options) txt = normalize_txt(txt) trues = options[:trues] trues ||= [normalize_txt(options[:true])] falses = options[:falses] falses ||= [normalize_txt(options[:falses])] @@ -172,10 +172,20 @@ # TODO: currency, money, bank accounts, credit card numbers, ... private # include ActionView::Helpers::NumberHelper + def number_format_options(options) + opt = I18n.translate(:'number.format', :locale => options[:locale]) + opt.kind_of?(Hash) ? opt : {:separator=>'.'} + end + + def logical_format_options(options) + opt = I18n.translate(:'logical.format', :locale => options[:locale]) + opt.kind_of?(Hash) ? opt : {:separator=>'.'} + end + def round(v, ndec) return v if (v.respond_to?(:nan?) && v.nan?) || (v.respond_to?(:infinite?) && v.infinite?) if ndec case v when BigDecimal @@ -188,15 +198,10 @@ v end # pos0 first digit, pos1 one past last integral digit def digit_grouping(txt,n,sep,pos0,pos1) - if sep - while pos1>pos0 - pos1 -= n - txt.insert pos1, sep if pos1>pos0 - end - end + txt[pos0...pos1] = txt[pos0...pos1].gsub(/(\d)(?=(#{'\\d'*n})+(?!\d))/, "\\1#{sep}") if sep txt end # Date-parsing has been taken from https://github.com/clemens/delocalize