diff options
author | Steve Klabnik <steve@steveklabnik.com> | 2013-08-12 12:30:11 -0700 |
---|---|---|
committer | Steve Klabnik <steve@steveklabnik.com> | 2013-08-12 12:30:11 -0700 |
commit | 1577d9661a9ee0268d9f101c6c7f6435a87e8de0 (patch) | |
tree | d7e13a728dd4381e13a9612baf1816397b17223e /activesupport/lib | |
parent | f948814b44e56643381f49ef0567fb44148f4efe (diff) | |
parent | af3ea544dd670409e80585329b019f598d29cef5 (diff) | |
download | rails-1577d9661a9ee0268d9f101c6c7f6435a87e8de0.tar.gz rails-1577d9661a9ee0268d9f101c6c7f6435a87e8de0.tar.bz2 rails-1577d9661a9ee0268d9f101c6c7f6435a87e8de0.zip |
Merge pull request #11856 from dchelimsky/refactor-duration-inspect
Refactor Duration#inspect
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index f7b12a1db6..00727ef73c 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -70,13 +70,13 @@ module ActiveSupport alias :until :ago def inspect #:nodoc: - consolidated = parts.inject(::Hash.new(0)) { |h,(l,r)| h[l] += r; h } - parts = [:years, :months, :days, :minutes, :seconds].map do |length| - n = consolidated[length] - "#{n} #{n == 1 ? length.to_s.chop : length.to_s}" if n.nonzero? - end.compact - parts = ["0 seconds"] if parts.empty? - parts.to_sentence(:locale => :en) + val_for = parts.inject(::Hash.new(0)) { |h,(l,r)| h[l] += r; h } + [:years, :months, :days, :minutes, :seconds]. + select {|unit| val_for[unit].nonzero?}. + tap {|units| units << :seconds if units.empty?}. + map {|unit| [unit, val_for[unit]]}. + map {|unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}"}. + to_sentence(:locale => :en) end def as_json(options = nil) #:nodoc: |