Class: ActiveSupport::Duration
- Inherits:
-
ProxyObject
- Object
- ProxyObject
- ActiveSupport::Duration
- Defined in:
- lib/active_support/duration.rb
Overview
Provides accurate date and time measurements using Date#advance and Time#advance, respectively. It mainly supports the methods on Numeric.
1.month.ago # equivalent to Time.now.advance(months: -1)
Instance Attribute Summary collapse
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.===(other) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Adds another Duration or a Numeric to this Duration.
-
#-(other) ⇒ Object
Subtracts another Duration or a Numeric from this Duration.
-
#-@ ⇒ Object
:nodoc:.
-
#==(other) ⇒ Object
Returns
true
ifother
is also a Duration instance with the samevalue
, or ifother == value
. -
#ago(time = ::Time.current) ⇒ Object
(also: #until)
Calculates a new Time or Date that is as far in the past as this Duration represents.
-
#as_json(options = nil) ⇒ Object
:nodoc:.
-
#initialize(value, parts) ⇒ Duration
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#is_a?(klass) ⇒ Boolean
(also: #kind_of?)
:nodoc:.
-
#since(time = ::Time.current) ⇒ Object
(also: #from_now)
Calculates a new Time or Date that is as far in the future as this Duration represents.
Methods inherited from ProxyObject
Constructor Details
#initialize(value, parts) ⇒ Duration
:nodoc:
13 14 15 |
# File 'lib/active_support/duration.rb', line 13 def initialize(value, parts) #:nodoc: @value, @parts = value, parts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
:nodoc:
112 113 114 |
# File 'lib/active_support/duration.rb', line 112 def method_missing(method, *args, &block) #:nodoc: value.send(method, *args, &block) end |
Instance Attribute Details
#parts ⇒ Object
Returns the value of attribute parts.
11 12 13 |
# File 'lib/active_support/duration.rb', line 11 def parts @parts end |
#value ⇒ Object
Returns the value of attribute value.
11 12 13 |
# File 'lib/active_support/duration.rb', line 11 def value @value end |
Class Method Details
Instance Method Details
#+(other) ⇒ Object
Adds another Duration or a Numeric to this Duration. Numeric values are treated as seconds.
19 20 21 22 23 24 25 |
# File 'lib/active_support/duration.rb', line 19 def +(other) if Duration === other Duration.new(value + other.value, @parts + other.parts) else Duration.new(value + other, @parts + [[:seconds, other]]) end end |
#-(other) ⇒ Object
Subtracts another Duration or a Numeric from this Duration. Numeric values are treated as seconds.
29 30 31 |
# File 'lib/active_support/duration.rb', line 29 def -(other) self + (-other) end |
#-@ ⇒ Object
:nodoc:
33 34 35 |
# File 'lib/active_support/duration.rb', line 33 def -@ #:nodoc: Duration.new(-value, parts.map { |type,number| [type, -number] }) end |
#==(other) ⇒ Object
Returns true
if other
is also a Duration instance with the same value
, or if other == value
.
44 45 46 47 48 49 50 |
# File 'lib/active_support/duration.rb', line 44 def ==(other) if Duration === other other.value == value else other == value end end |
#ago(time = ::Time.current) ⇒ Object Also known as: until
Calculates a new Time or Date that is as far in the past as this Duration represents.
67 68 69 |
# File 'lib/active_support/duration.rb', line 67 def ago(time = ::Time.current) sum(-1, time) end |
#as_json(options = nil) ⇒ Object
:nodoc:
82 83 84 |
# File 'lib/active_support/duration.rb', line 82 def as_json( = nil) #:nodoc: to_i end |
#inspect ⇒ Object
:nodoc:
72 73 74 75 76 77 78 79 80 |
# File 'lib/active_support/duration.rb', line 72 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.singularize : length.to_s}" if n.nonzero? end.compact parts = ["0 seconds"] if parts.empty? parts.to_sentence(:locale => :en) end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
:nodoc:
37 38 39 |
# File 'lib/active_support/duration.rb', line 37 def is_a?(klass) #:nodoc: Duration == klass || value.is_a?(klass) end |