Class: ActiveSupport::Duration

Inherits:
ProxyObject show all
Defined in:
activesupport/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)

Methods inherited from ProxyObject

#raise

Constructor Details

- (Duration) initialize(value, parts)

:nodoc:



13
14
15
# File 'activesupport/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

- (Object) method_missing(method, *args, &block) (private)

:nodoc:



114
115
116
# File 'activesupport/lib/active_support/duration.rb', line 114

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

Instance Attribute Details

- (Object) parts

Returns the value of attribute parts



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

def parts
  @parts
end

- (Object) value

Returns the value of attribute value



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

def value
  @value
end

Class Method Details

+ (Object) ===(other)

:nodoc:



56
57
58
59
60
# File 'activesupport/lib/active_support/duration.rb', line 56

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

Instance Method Details

- (Object) +(other)

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



19
20
21
22
23
24
25
# File 'activesupport/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

- (Object) -(other)

Subtracts another Duration or a Numeric from this Duration. Numeric values are treated as seconds.



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

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

- (Object) -@

:nodoc:



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

def -@ #:nodoc:
  Duration.new(-value, parts.map { |type,number| [type, -number] })
end

- (Object) ==(other)

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 'activesupport/lib/active_support/duration.rb', line 44

def ==(other)
  if Duration === other
    other.value == value
  else
    other == value
  end
end

- (Object) ago(time = ::Time.current) Also known as: until

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



71
72
73
# File 'activesupport/lib/active_support/duration.rb', line 71

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

- (Object) as_json(options = nil)

:nodoc:



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

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

- (Boolean) eql?(other)



52
53
54
# File 'activesupport/lib/active_support/duration.rb', line 52

def eql?(other)
  other.is_a?(Duration) && self == other
end

- (Object) inspect

:nodoc:



76
77
78
79
80
81
82
# File 'activesupport/lib/active_support/duration.rb', line 76

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 => :en)
end

- (Boolean) is_a?(klass) Also known as: kind_of?

:nodoc:



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

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

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

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



64
65
66
# File 'activesupport/lib/active_support/duration.rb', line 64

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