diff options
Diffstat (limited to 'activesupport/lib/active_support/duration.rb')
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index 87b6407038..1db0ca51ae 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -81,6 +81,18 @@ module ActiveSupport to_i end + # Flattens all the +parts+ of the duration, and returns a + # new duration object. + def flatten + Duration.new(@value, flatten_parts) + end + + # Flattens all the +parts+ of this duration. + def flatten! + @parts = flatten_parts + self + end + protected def sum(sign, time = ::Time.current) #:nodoc: @@ -97,6 +109,14 @@ module ActiveSupport end end + def flatten_parts + @parts.inject({}) do |result, (name, value)| + result[name] ||= 0 + result[name] += value + result + end.to_a + end + private def method_missing(method, *args, &block) #:nodoc: |