mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove code duplication
This commit is contained in:
parent
30b0e5848c
commit
f170453493
1 changed files with 19 additions and 34 deletions
|
@ -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 <tt>:precision</tt> (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
|
||||
|
|
Loading…
Reference in a new issue