Class: Timedelta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/units-time/timedelta.rb

Defined Under Namespace

Classes: SafeInteger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds = 0) ⇒ Timedelta

Returns a new instance of Timedelta.



8
9
10
# File 'lib/units-time/timedelta.rb', line 8

def initialize( seconds=0 )
  @seconds = seconds
end

Instance Attribute Details

#secondsObject (readonly) Also known as: to_i, to_int

Returns the value of attribute seconds.



6
7
8
# File 'lib/units-time/timedelta.rb', line 6

def seconds
  @seconds
end

Class Method Details

.zeroObject

todo/fix: always freeze by default (timedelta is immutable) - why? why not?



85
# File 'lib/units-time/timedelta.rb', line 85

def self.zero()  @@zero ||= new(0).freeze; end

Instance Method Details

#*(other) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/units-time/timedelta.rb', line 22

def *( other )
  if other.is_a?( Integer )
    self.class.new( @seconds * other )
  else
    raise TypeError.new( "[Timedelta] mul(tiplication) - wrong type >#{other.inspect}< #{other.class.name} - integer number expected" )
  end
end

#+(other) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/units-time/timedelta.rb', line 30

def +( other )
  if other.is_a?( self.class )
    self.class.new( @seconds + other.seconds )
  else
    raise TypeError.new( "[Timedelta] add(ition) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#-(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/units-time/timedelta.rb', line 38

def -( other )
  if other.is_a?( self.class )
    self.class.new( @seconds - other.seconds )
  else
    raise TypeError.new( "[Timedelta] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#<=>(other) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/units-time/timedelta.rb', line 75

def <=>( other )
  if other.is_a?( self.class )
    @seconds <=> other.seconds
  else
    raise TypeError.new( "[Timedelta] <=> - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#==(other) ⇒ Object Also known as: eql?



13
14
15
16
17
18
19
# File 'lib/units-time/timedelta.rb', line 13

def ==( other )
  if other.is_a?( self.class )
    @seconds == other.seconds
  else
    false
  end
end

#coerce(other) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/units-time/timedelta.rb', line 64

def coerce( other )
  if other.is_a?( Integer )
    [SafeInteger.new(other), self]
  else
    raise TypeError.new( "[Timedelta] coerce - wrong type >#{other.inspect}< #{other.class.name} - Integer number expected" )
  end
end