Class: Astronoby::Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/astronoby/duration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds) ⇒ Duration

Returns a new instance of Duration.

Parameters:

  • seconds (Numeric)

    the duration in seconds



45
46
47
48
# File 'lib/astronoby/duration.rb', line 45

def initialize(seconds)
  @seconds = seconds
  freeze
end

Instance Attribute Details

#secondsNumeric (readonly)

Returns the duration in seconds.

Returns:

  • (Numeric)

    the duration in seconds



42
43
44
# File 'lib/astronoby/duration.rb', line 42

def seconds
  @seconds
end

Class Method Details

.from_days(days) ⇒ Astronoby::Duration

Returns a new Duration.

Parameters:

  • days (Numeric)

    the duration in days

Returns:



35
36
37
38
# File 'lib/astronoby/duration.rb', line 35

def from_days(days)
  seconds = days * Constants::SECONDS_PER_DAY
  from_seconds(seconds)
end

.from_hours(hours) ⇒ Astronoby::Duration

Returns a new Duration.

Parameters:

  • hours (Numeric)

    the duration in hours

Returns:



28
29
30
31
# File 'lib/astronoby/duration.rb', line 28

def from_hours(hours)
  seconds = hours * Constants::SECONDS_PER_HOUR
  from_seconds(seconds)
end

.from_minutes(minutes) ⇒ Astronoby::Duration

Returns a new Duration.

Parameters:

  • minutes (Numeric)

    the duration in minutes

Returns:



21
22
23
24
# File 'lib/astronoby/duration.rb', line 21

def from_minutes(minutes)
  seconds = minutes * Constants::SECONDS_PER_MINUTE
  from_seconds(seconds)
end

.from_seconds(seconds) ⇒ Astronoby::Duration

Returns a new Duration.

Parameters:

  • seconds (Numeric)

    the duration in seconds

Returns:



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

def from_seconds(seconds)
  new(seconds)
end

.zeroAstronoby::Duration

Returns a zero duration.

Returns:



9
10
11
# File 'lib/astronoby/duration.rb', line 9

def zero
  new(0)
end

Instance Method Details

#+(other) ⇒ Astronoby::Duration

Returns the sum.

Parameters:

Returns:



67
68
69
# File 'lib/astronoby/duration.rb', line 67

def +(other)
  self.class.from_seconds(@seconds + other.seconds)
end

#-(other) ⇒ Astronoby::Duration

Returns the difference.

Parameters:

Returns:



73
74
75
# File 'lib/astronoby/duration.rb', line 73

def -(other)
  self.class.from_seconds(@seconds - other.seconds)
end

#-@Astronoby::Duration

Returns the negated duration.

Returns:



78
79
80
# File 'lib/astronoby/duration.rb', line 78

def -@
  self.class.from_seconds(-@seconds)
end

#<=>(other) ⇒ Integer?

Returns -1, 0, or 1; nil if not comparable.

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, or 1; nil if not comparable



109
110
111
112
113
# File 'lib/astronoby/duration.rb', line 109

def <=>(other)
  return unless other.is_a?(self.class)

  seconds <=> other.seconds
end

#absAstronoby::Duration

Returns the absolute duration.

Returns:



83
84
85
# File 'lib/astronoby/duration.rb', line 83

def abs
  self.class.from_seconds(@seconds.abs)
end

#daysFloat

Returns the duration in days.

Returns:

  • (Float)

    the duration in days



61
62
63
# File 'lib/astronoby/duration.rb', line 61

def days
  @seconds / Constants::SECONDS_PER_DAY
end

#hashInteger

Returns hash value.

Returns:

  • (Integer)

    hash value



103
104
105
# File 'lib/astronoby/duration.rb', line 103

def hash
  [@seconds, self.class].hash
end

#hoursFloat

Returns the duration in hours.

Returns:

  • (Float)

    the duration in hours



56
57
58
# File 'lib/astronoby/duration.rb', line 56

def hours
  @seconds / Constants::SECONDS_PER_HOUR
end

#minutesFloat

Returns the duration in minutes.

Returns:

  • (Float)

    the duration in minutes



51
52
53
# File 'lib/astronoby/duration.rb', line 51

def minutes
  @seconds / Constants::SECONDS_PER_MINUTE
end

#negative?Boolean

Returns true if the duration is negative.

Returns:

  • (Boolean)

    true if the duration is negative



93
94
95
# File 'lib/astronoby/duration.rb', line 93

def negative?
  @seconds < 0
end

#positive?Boolean

Returns true if the duration is positive.

Returns:

  • (Boolean)

    true if the duration is positive



88
89
90
# File 'lib/astronoby/duration.rb', line 88

def positive?
  @seconds > 0
end

#zero?Boolean

Returns true if the duration is zero.

Returns:

  • (Boolean)

    true if the duration is zero



98
99
100
# File 'lib/astronoby/duration.rb', line 98

def zero?
  @seconds.zero?
end