Class: ActiveSupport::Duration

Inherits:
BasicObject
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. Example:

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 BasicObject

#raise

Constructor Details

- (Duration) initialize(value, parts)

:nodoc:



15
16
17
# File 'activesupport/lib/active_support/duration.rb', line 15

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:



106
107
108
# File 'activesupport/lib/active_support/duration.rb', line 106

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

Instance Attribute Details

- (Object) parts

Returns the value of attribute parts



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

def parts
  @parts
end

- (Object) value

Returns the value of attribute value



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

def value
  @value
end

Class Method Details

+ (Object) ===(other)

:nodoc:



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

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.



21
22
23
24
25
26
27
# File 'activesupport/lib/active_support/duration.rb', line 21

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.



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

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

- (Object) -@

:nodoc:



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

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.



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

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.



69
70
71
# File 'activesupport/lib/active_support/duration.rb', line 69

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

- (Object) inspect

:nodoc:



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

def inspect #:nodoc:
  consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; 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

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

:nodoc:

Returns:

  • (Boolean)


39
40
41
# File 'activesupport/lib/active_support/duration.rb', line 39

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.



62
63
64
# File 'activesupport/lib/active_support/duration.rb', line 62

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