diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 16:37:39 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-10-20 16:37:39 -0600 |
commit | 7e434d6d2d3e8056e3024f6a3ec2db9e0365d10a (patch) | |
tree | e8b8ebf14ff9319615c7bcf60da950379e0d635e /activesupport/lib | |
parent | 286c1c3aac856d89d2470b738ba8abfdf230fbff (diff) | |
parent | f61dd4f9d4c48b1d447b499d0541d2f6421aedd7 (diff) | |
download | rails-7e434d6d2d3e8056e3024f6a3ec2db9e0365d10a.tar.gz rails-7e434d6d2d3e8056e3024f6a3ec2db9e0365d10a.tar.bz2 rails-7e434d6d2d3e8056e3024f6a3ec2db9e0365d10a.zip |
Merge pull request #20038 from imanel/numeric_prepend
Use Module.prepend instead of alias_method and unify behavior of all Numeric extensions
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/big_decimal/conversions.rb | 15 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/numeric/conversions.rb | 37 |
2 files changed, 19 insertions, 33 deletions
diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 234283e792..22fc7ecf92 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -1,15 +1,14 @@ require 'bigdecimal' require 'bigdecimal/util' -class BigDecimal - DEFAULT_STRING_FORMAT = 'F' - alias_method :to_default_s, :to_s +module ActiveSupport + module BigDecimalWithDefaultFormat #:nodoc: + DEFAULT_STRING_FORMAT = 'F' - def to_s(format = nil, options = nil) - if format.is_a?(Symbol) - to_formatted_s(format, options || {}) - else - to_default_s(format || DEFAULT_STRING_FORMAT) + def to_s(format = nil) + super(format || DEFAULT_STRING_FORMAT) end end end + +BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat) diff --git a/activesupport/lib/active_support/core_ext/numeric/conversions.rb b/activesupport/lib/active_support/core_ext/numeric/conversions.rb index d5bb11deed..9a3651f29a 100644 --- a/activesupport/lib/active_support/core_ext/numeric/conversions.rb +++ b/activesupport/lib/active_support/core_ext/numeric/conversions.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext/big_decimal/conversions' require 'active_support/number_helper' -class Numeric +module ActiveSupport::NumericWithFormat # Provides options for converting numbers into formatted strings. # Options are provided for phone numbers, currency, percentage, @@ -97,7 +97,10 @@ class Numeric # 1234567.to_s(:human, precision: 1, # separator: ',', # significant: false) # => "1,2 Million" - def to_formatted_s(format = :default, options = {}) + def to_s(*args) + format, options = args + options ||= {} + case format when :phone return ActiveSupport::NumberHelper.number_to_phone(self, options) @@ -114,32 +117,16 @@ class Numeric when :human_size return ActiveSupport::NumberHelper.number_to_human_size(self, options) else - self.to_default_s - end - end - - [Fixnum, Bignum].each do |klass| - klass.class_eval do - alias_method :to_default_s, :to_s - def to_s(base_or_format = 10, options = nil) - if base_or_format.is_a?(Symbol) - to_formatted_s(base_or_format, options || {}) - else - to_default_s(base_or_format) - end - end + super end end - Float.class_eval do - alias_method :to_default_s, :to_s - def to_s(*args) - if args.empty? - to_default_s - else - to_formatted_s(*args) - end - end + def to_formatted_s(*args) + to_s(*args) end + deprecate to_formatted_s: :to_s +end +[Fixnum, Bignum, Float, BigDecimal].each do |klass| + klass.prepend(ActiveSupport::NumericWithFormat) end |