Class: ActiveSupport::Duration

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, parts) ⇒ Duration

:nodoc:



12
13
14
# File 'lib/active_support/duration.rb', line 12

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:



156
157
158
# File 'lib/active_support/duration.rb', line 156

def method_missing(method, *args, &block) #:nodoc:
  value.send(method, *args, &block)
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



10
11
12
# File 'lib/active_support/duration.rb', line 10

def parts
  @parts
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/active_support/duration.rb', line 10

def value
  @value
end

Class Method Details

.===(other) ⇒ Object

:nodoc:



93
94
95
96
97
# File 'lib/active_support/duration.rb', line 93

def self.===(other) #:nodoc:
  other.is_a?(Duration)
rescue ::NoMethodError
  false
end

Instance Method Details

#+(other) ⇒ Object

Adds another Duration or a Numeric to this Duration. Numeric values are treated as seconds.



18
19
20
21
22
23
24
# File 'lib/active_support/duration.rb', line 18

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.



28
29
30
# File 'lib/active_support/duration.rb', line 28

def -(other)
  self + (-other)
end

#-@Object

:nodoc:



32
33
34
# File 'lib/active_support/duration.rb', line 32

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.



47
48
49
50
51
52
53
# File 'lib/active_support/duration.rb', line 47

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.



108
109
110
# File 'lib/active_support/duration.rb', line 108

def ago(time = ::Time.current)
  sum(-1, time)
end

#as_json(options = nil) ⇒ Object

:nodoc:



121
122
123
# File 'lib/active_support/duration.rb', line 121

def as_json(options = nil) #:nodoc:
  to_i
end

#eql?(other) ⇒ Boolean

Returns true if other is also a Duration instance, which has the same parts as this one.

Returns:

  • (Boolean)


85
86
87
# File 'lib/active_support/duration.rb', line 85

def eql?(other)
  Duration === other && other.value.eql?(value)
end

#hashObject



89
90
91
# File 'lib/active_support/duration.rb', line 89

def hash
  @value.hash
end

#inspectObject

:nodoc:



113
114
115
116
117
118
119
# File 'lib/active_support/duration.rb', line 113

def inspect #:nodoc:
  parts.
    reduce(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }.
    sort_by {|unit,  _ | [:years, :months, :days, :minutes, :seconds].index(unit)}.
    map     {|unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}"}.
    to_sentence(locale: ::I18n.default_locale)
end

#instance_of?(klass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_support/duration.rb', line 41

def instance_of?(klass) # :nodoc:
  Duration == klass || value.instance_of?(klass)
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

:nodoc:

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_support/duration.rb', line 36

def is_a?(klass) #:nodoc:
  Duration == klass || value.is_a?(klass)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

:nodoc

Returns:

  • (Boolean)


125
126
127
# File 'lib/active_support/duration.rb', line 125

def respond_to_missing?(method, include_private=false) #:nodoc
  @value.respond_to?(method, include_private)
end

#since(time = ::Time.current) ⇒ Object Also known as: from_now

Calculates a new Time or Date that is as far in the future as this Duration represents.



101
102
103
# File 'lib/active_support/duration.rb', line 101

def since(time = ::Time.current)
  sum(1, time)
end

#to_iObject

Returns the number of seconds that this Duration represents.

1.minute.to_i   # => 60
1.hour.to_i     # => 3600
1.day.to_i      # => 86400

Note that this conversion makes some assumptions about the duration of some periods, e.g. months are always 30 days and years are 365.25 days:

# equivalent to 30.days.to_i
1.month.to_i    # => 2592000

# equivalent to 365.25.days.to_i
1.year.to_i     # => 31557600

In such cases, Ruby’s core Date and Time should be used for precision date and time arithmetic.



79
80
81
# File 'lib/active_support/duration.rb', line 79

def to_i
  @value.to_i
end

#to_sObject



55
56
57
# File 'lib/active_support/duration.rb', line 55

def to_s
  @value.to_s
end