diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2013-01-04 00:31:47 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2013-01-04 00:32:52 -0200 |
commit | dd3360e05e4909f2f0c74a624cccc2def688f828 (patch) | |
tree | 66f4ca20dbd9311e5a56fcf61832ffcd57257c66 /activesupport | |
parent | 05ef97dd141c02103d58d67ab8339bcc6a1fe0e8 (diff) | |
download | rails-dd3360e05e4909f2f0c74a624cccc2def688f828.tar.gz rails-dd3360e05e4909f2f0c74a624cccc2def688f828.tar.bz2 rails-dd3360e05e4909f2f0c74a624cccc2def688f828.zip |
Refactor infinite comparable definition a bit
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/infinite_comparable.rb | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/activesupport/lib/active_support/core_ext/infinite_comparable.rb b/activesupport/lib/active_support/core_ext/infinite_comparable.rb index a69ff6a37b..df9cde7db0 100644 --- a/activesupport/lib/active_support/core_ext/infinite_comparable.rb +++ b/activesupport/lib/active_support/core_ext/infinite_comparable.rb @@ -1,4 +1,5 @@ require 'active_support/concern' +require 'active_support/core_ext/object/try' module InfiniteComparable extend ActiveSupport::Concern @@ -7,24 +8,27 @@ module InfiniteComparable alias_method_chain :<=>, :infinity end - define_method '<=>_with_infinity' do |other| + define_method :'<=>_with_infinity' do |other| if other.class == self.class - self.send(:'<=>_without_infinity', other) - # inf <=> inf - elsif other.respond_to?(:infinite?) && other.infinite? && respond_to?(:infinite?) && infinite? - infinite? <=> other.infinite? - # not_inf <=> inf - elsif other.respond_to?(:infinite?) && other.infinite? - -other.infinite? - # inf <=> not_inf - elsif respond_to?(:infinite?) && infinite? - infinite? + public_send :'<=>_without_infinity', other else - conversion = :"to_#{self.class.name.downcase}" + infinite = try(:infinite?) + other_infinite = other.try(:infinite?) - other = other.send(conversion) if other.respond_to?(conversion) - - self.send(:'<=>_without_infinity', other) + # inf <=> inf + if infinite && other_infinite + infinite <=> other_infinite + # not_inf <=> inf + elsif other_infinite + -other_infinite + # inf <=> not_inf + elsif infinite + infinite + else + conversion = "to_#{self.class.name.downcase}" + other = other.public_send(conversion) if other.respond_to?(conversion) + public_send :'<=>_without_infinity', other + end end end end |