Class: ScheduledValue::Timespan
- Inherits:
-
Object
- Object
- ScheduledValue::Timespan
- Includes:
- Comparable
- Defined in:
- lib/scheduled_value/timespan.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#finish ⇒ Object
Returns the value of attribute finish.
-
#start ⇒ Object
Returns the value of attribute start.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #attributes ⇒ Object
- #contains?(timestamp) ⇒ Boolean
- #finish_description(format = nil, timezone = nil) ⇒ Object
-
#initialize(start: nil, finish: nil) ⇒ Timespan
constructor
A new instance of Timespan.
- #inspect ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #start_description(format = nil, timezone = nil) ⇒ Object
- #to_hash ⇒ Object
- #to_s(format = nil, timezone = nil) ⇒ Object
Constructor Details
#initialize(start: nil, finish: nil) ⇒ Timespan
Returns a new instance of Timespan.
9 10 11 12 13 14 |
# File 'lib/scheduled_value/timespan.rb', line 9 def initialize(start: nil, finish: nil) self.start = start self.finish = finish raise "Finish must be after start" if start && finish && start >= finish end |
Instance Attribute Details
#finish ⇒ Object
Returns the value of attribute finish.
7 8 9 |
# File 'lib/scheduled_value/timespan.rb', line 7 def finish @finish end |
#start ⇒ Object
Returns the value of attribute start.
7 8 9 |
# File 'lib/scheduled_value/timespan.rb', line 7 def start @start end |
Instance Method Details
#<=>(other) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/scheduled_value/timespan.rb', line 49 def <=>(other) case other when Timespan then compare_timespan(other) when Date, Time, DateTime then compare_datetime(other) end end |
#attributes ⇒ Object
16 17 18 19 20 21 |
# File 'lib/scheduled_value/timespan.rb', line 16 def attributes { start: start, finish: finish } end |
#contains?(timestamp) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/scheduled_value/timespan.rb', line 35 def contains?() return false if start && < start return false if finish && >= finish true end |
#finish_description(format = nil, timezone = nil) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/scheduled_value/timespan.rb', line 73 def finish_description(format = nil, timezone = nil) if finish finish_in_zone = timezone ? finish.in_time_zone(timezone) : finish "up to #{finish_in_zone.to_s(format)}" elsif start "indefinitely" end end |
#inspect ⇒ Object
56 57 58 |
# File 'lib/scheduled_value/timespan.rb', line 56 def inspect "#<#{self.class.name}: #{self}>" end |
#overlaps?(other) ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/scheduled_value/timespan.rb', line 42 def overlaps?(other) return false if finish && other.start && other.start >= finish return false if start && other.finish && start >= other.finish true end |
#start_description(format = nil, timezone = nil) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/scheduled_value/timespan.rb', line 64 def start_description(format = nil, timezone = nil) if start start_in_zone = timezone ? start.in_time_zone(timezone) : start "from #{start_in_zone.to_s(format)}" else 'anytime' end end |
#to_hash ⇒ Object
23 24 25 |
# File 'lib/scheduled_value/timespan.rb', line 23 def to_hash(*) attributes end |
#to_s(format = nil, timezone = nil) ⇒ Object
60 61 62 |
# File 'lib/scheduled_value/timespan.rb', line 60 def to_s(format = nil, timezone = nil) "#{start_description(format, timezone)} #{finish_description(format, timezone)}" end |