Class: TimeInterval::TimeWithDuration
- Inherits:
-
Object
- Object
- TimeInterval::TimeWithDuration
- Defined in:
- lib/time_interval/time_with_duration.rb
Constant Summary collapse
- UNITS =
%w(years months weeks days hours minutes seconds).freeze
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #end_time ⇒ Object
-
#initialize(time, duration, forwards = true) ⇒ TimeWithDuration
constructor
A new instance of TimeWithDuration.
- #iso8601 ⇒ Object
- #step ⇒ Object
Constructor Details
#initialize(time, duration, forwards = true) ⇒ TimeWithDuration
Returns a new instance of TimeWithDuration.
28 29 30 31 32 |
# File 'lib/time_interval/time_with_duration.rb', line 28 def initialize(time, duration, forwards = true) @start_time = time @duration = duration @forwards = forwards end |
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
26 27 28 |
# File 'lib/time_interval/time_with_duration.rb', line 26 def duration @duration end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
25 26 27 |
# File 'lib/time_interval/time_with_duration.rb', line 25 def start_time @start_time end |
Class Method Details
.parse(iso8601) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/time_interval/time_with_duration.rb', line 5 def self.parse(iso8601) halves = iso8601.split('/') fail ArgumentError unless halves.length == 2 if halves[0]['P'] forwards = false duration = Duration.parse(halves[0]) time = halves[1] elsif halves[1]['P'] forwards = true duration = Duration.parse(halves[1]) time = halves[0] else fail ArgumentError end new Time.parse(time), duration, forwards end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
34 35 36 37 38 |
# File 'lib/time_interval/time_with_duration.rb', line 34 def ==(other) return false unless other.is_a? TimeWithDuration start_time == other.start_time && end_time == other.end_time end |
#end_time ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/time_interval/time_with_duration.rb', line 42 def end_time if forwards? duration.add_to start_time else duration.subtract_from start_time end end |
#iso8601 ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/time_interval/time_with_duration.rb', line 50 def iso8601 if forwards? "#{start_time.iso8601}/#{duration.iso8601}" else "#{duration.iso8601}/#{start_time.iso8601}" end end |
#step ⇒ Object
58 59 60 |
# File 'lib/time_interval/time_with_duration.rb', line 58 def step TimeWithDuration.new end_time, duration, forwards? end |