aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorIonatan Wiznia <ionatan.wiznia@traddia.com>2013-10-11 12:50:23 +0200
committerIonatan Wiznia <ionatan.wiznia@traddia.com>2013-10-11 12:50:23 +0200
commit90871f7198f0739c82df2f1bbb66c5f8dd5a720a (patch)
tree334ed4365a5948da0aaacbdd692338f280c17244 /activesupport/lib/active_support
parent0061c5e1ef0f7be8946602456a538c263fcafed2 (diff)
downloadrails-90871f7198f0739c82df2f1bbb66c5f8dd5a720a.tar.gz
rails-90871f7198f0739c82df2f1bbb66c5f8dd5a720a.tar.bz2
rails-90871f7198f0739c82df2f1bbb66c5f8dd5a720a.zip
flatten and flatten! methods for ActiveSupport::Duration
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/duration.rb20
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: