class AS::Duration

Attributes

parts[RW]
value[RW]

Public Class Methods

new(value, parts) click to toggle source
# File lib/as/duration.rb, line 10
def initialize(value, parts)
  @value, @parts = value, parts
end

Public Instance Methods

+(other) click to toggle source
# File lib/as/duration.rb, line 29
def +(other)
  raise TypeError, "can only add Duration objects" if not Duration === other
  Duration.new(value + other.value, parts + other.parts)
end
-(other) click to toggle source
# File lib/as/duration.rb, line 34
def -(other)
  raise TypeError, "can only subtract Duration objects" if not Duration === other
  self + (-other)
end
-@() click to toggle source
# File lib/as/duration.rb, line 39
def -@
  Duration.new(-value, parts.map { |type, number| [type, -number] })
end
<=>(other) click to toggle source
# File lib/as/duration.rb, line 24
def <=>(other)
  return nil if not Duration === other
  self.value <=> other.value
end
after(time)
Alias for: from
ago() click to toggle source
# File lib/as/duration.rb, line 59
def ago
  self.until(Time.now)
end
before(time)
Alias for: until
from(time) click to toggle source
# File lib/as/duration.rb, line 43
def from(time)
  advance(time)
end
Also aliased as: since, after
from_now() click to toggle source
# File lib/as/duration.rb, line 49
def from_now
  from(Time.now)
end
since(time)
Alias for: from
to(time)
Alias for: until
to_f() click to toggle source

reference: Rails–>activesupport/lib/active_support/duration.rb Add this method FOR something like 5.minutes.to_f, which is used in ActiveSupport::Cache::Entry#initialize.

# File lib/as/duration.rb, line 20
def to_f
  @value.to_f
end
to_i() click to toggle source
# File lib/as/duration.rb, line 14
def to_i
  @value
end
until(time) click to toggle source
# File lib/as/duration.rb, line 53
def until(time)
  (-self).advance(time)
end
Also aliased as: to, before

Protected Instance Methods

advance(time) click to toggle source
# File lib/as/duration.rb, line 65
def advance(time)
  Calculator.new(parts).advance(time)
end