Class: Astronoby::Duration
- Inherits:
-
Object
- Object
- Astronoby::Duration
- Includes:
- Comparable
- Defined in:
- lib/astronoby/duration.rb
Instance Attribute Summary collapse
-
#seconds ⇒ Numeric
readonly
The duration in seconds.
Class Method Summary collapse
-
.from_days(days) ⇒ Astronoby::Duration
A new Duration.
-
.from_hours(hours) ⇒ Astronoby::Duration
A new Duration.
-
.from_minutes(minutes) ⇒ Astronoby::Duration
A new Duration.
-
.from_seconds(seconds) ⇒ Astronoby::Duration
A new Duration.
-
.zero ⇒ Astronoby::Duration
A zero duration.
Instance Method Summary collapse
-
#+(other) ⇒ Astronoby::Duration
The sum.
-
#-(other) ⇒ Astronoby::Duration
The difference.
-
#-@ ⇒ Astronoby::Duration
The negated duration.
-
#<=>(other) ⇒ Integer?
-1, 0, or 1; nil if not comparable.
-
#abs ⇒ Astronoby::Duration
The absolute duration.
-
#days ⇒ Float
The duration in days.
-
#hash ⇒ Integer
Hash value.
-
#hours ⇒ Float
The duration in hours.
-
#initialize(seconds) ⇒ Duration
constructor
A new instance of Duration.
-
#minutes ⇒ Float
The duration in minutes.
-
#negative? ⇒ Boolean
True if the duration is negative.
-
#positive? ⇒ Boolean
True if the duration is positive.
-
#zero? ⇒ Boolean
True if the duration is zero.
Constructor Details
#initialize(seconds) ⇒ Duration
Returns a new instance of Duration.
45 46 47 48 |
# File 'lib/astronoby/duration.rb', line 45 def initialize(seconds) @seconds = seconds freeze end |
Instance Attribute Details
#seconds ⇒ Numeric (readonly)
Returns 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.
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.
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.
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.
15 16 17 |
# File 'lib/astronoby/duration.rb', line 15 def from_seconds(seconds) new(seconds) end |
.zero ⇒ Astronoby::Duration
Returns a zero duration.
9 10 11 |
# File 'lib/astronoby/duration.rb', line 9 def zero new(0) end |
Instance Method Details
#+(other) ⇒ Astronoby::Duration
Returns the sum.
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.
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.
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.
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 |
#abs ⇒ Astronoby::Duration
Returns the absolute duration.
83 84 85 |
# File 'lib/astronoby/duration.rb', line 83 def abs self.class.from_seconds(@seconds.abs) end |
#days ⇒ Float
Returns the duration in days.
61 62 63 |
# File 'lib/astronoby/duration.rb', line 61 def days @seconds / Constants::SECONDS_PER_DAY end |
#hash ⇒ Integer
Returns hash value.
103 104 105 |
# File 'lib/astronoby/duration.rb', line 103 def hash [@seconds, self.class].hash end |
#hours ⇒ Float
Returns the duration in hours.
56 57 58 |
# File 'lib/astronoby/duration.rb', line 56 def hours @seconds / Constants::SECONDS_PER_HOUR end |
#minutes ⇒ Float
Returns 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.
93 94 95 |
# File 'lib/astronoby/duration.rb', line 93 def negative? @seconds < 0 end |
#positive? ⇒ Boolean
Returns 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.
98 99 100 |
# File 'lib/astronoby/duration.rb', line 98 def zero? @seconds.zero? end |