Class: AS::Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/as/duration.rb,
lib/as/duration/operations.rb

Defined Under Namespace

Modules: Operations Classes: Calculator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, parts) ⇒ Duration

Returns a new instance of Duration.



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

def initialize(value, parts)
  @value, @parts = value, parts
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



8
9
10
# File 'lib/as/duration.rb', line 8

def parts
  @parts
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/as/duration.rb', line 8

def value
  @value
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (TypeError)


29
30
31
32
# 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) ⇒ Object

Raises:

  • (TypeError)


34
35
36
37
# File 'lib/as/duration.rb', line 34

def -(other)
  raise TypeError, "can only subtract Duration objects" if not Duration === other
  self + (-other)
end

#-@Object



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

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

#<=>(other) ⇒ Object



24
25
26
27
# File 'lib/as/duration.rb', line 24

def <=>(other)
  return nil if not Duration === other
  self.value <=> other.value
end

#agoObject



59
60
61
# File 'lib/as/duration.rb', line 59

def ago
  self.until(Time.now)
end

#from(time) ⇒ Object Also known as: since, after



43
44
45
# File 'lib/as/duration.rb', line 43

def from(time)
  advance(time)
end

#from_nowObject



49
50
51
# File 'lib/as/duration.rb', line 49

def from_now
  from(Time.now)
end

#to_fObject

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.



20
21
22
# File 'lib/as/duration.rb', line 20

def to_f
  @value.to_f
end

#to_iObject



14
15
16
# File 'lib/as/duration.rb', line 14

def to_i
  @value
end

#until(time) ⇒ Object Also known as: to, before



53
54
55
# File 'lib/as/duration.rb', line 53

def until(time)
  (-self).advance(time)
end