From f170453493dd7c2d62f7b866867f7a3c8ec623d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 20 Jan 2012 16:26:45 -0300 Subject: [PATCH] Remove code duplication --- .../lib/action_view/helpers/number_helper.rb | 53 +++++++------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index c560750927..d4f1c22c14 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -237,14 +237,8 @@ module ActionView def number_with_delimiter(number, options = {}) options.symbolize_keys! - begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -253,7 +247,6 @@ module ActionView parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") parts.join(options[:separator]).html_safe - end # Formats a +number+ with the specified level of :precision (e.g., 112.32 has a precision @@ -290,14 +283,8 @@ module ActionView def number_with_precision(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -329,7 +316,6 @@ module ActionView else formatted_number end - end STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb].freeze @@ -367,14 +353,8 @@ module ActionView def number_to_human_size(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -486,14 +466,8 @@ module ActionView def number_to_human(number, options = {}) options.symbolize_keys! - number = begin - Float(number) - rescue ArgumentError, TypeError - if options[:raise] - raise InvalidNumberError, number - else - return number - end + number = parse_float_number(number, options[:raise]) do + return number end defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) @@ -536,6 +510,17 @@ module ActionView decimal_format.gsub(/%n/, formatted_number).gsub(/%u/, unit).strip.html_safe end + private + + def parse_float_number(number, raise_error) + Float(number) + rescue ArgumentError, TypeError + if raise_error + raise InvalidNumberError, number + else + yield + end + end end end end