Class: Achoo::Timespan
- Inherits:
-
Object
- Object
- Achoo::Timespan
- Defined in:
- lib/achoo/timespan.rb
Direct Known Subclasses
Constant Summary collapse
- SECONDS_IN_A_DAY =
86400
- SECONDS_IN_AN_HOUR =
3600
- SECONDS_IN_A_MINUTE =
60
Instance Attribute Summary collapse
-
#end ⇒ Object
Returns the value of attribute end.
-
#start ⇒ Object
Returns the value of attribute start.
Instance Method Summary collapse
- #contains?(timeish_or_timespan) ⇒ Boolean
-
#initialize(start, end_) ⇒ Timespan
constructor
A new instance of Timespan.
- #overlaps?(timespan) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(start, end_) ⇒ Timespan
Returns a new instance of Timespan.
14 15 16 17 18 19 |
# File 'lib/achoo/timespan.rb', line 14 def initialize(start, end_) raise ArgumentError.new('Nil in parameters not allowed') if start.nil? || end_.nil? self.start = start self.end = end_ end |
Instance Attribute Details
#end ⇒ Object
Returns the value of attribute end.
12 13 14 |
# File 'lib/achoo/timespan.rb', line 12 def end @end end |
#start ⇒ Object
Returns the value of attribute start.
11 12 13 |
# File 'lib/achoo/timespan.rb', line 11 def start @start end |
Instance Method Details
#contains?(timeish_or_timespan) ⇒ Boolean
36 37 38 39 40 41 42 43 44 |
# File 'lib/achoo/timespan.rb', line 36 def contains?(timeish_or_timespan) if timeish_or_timespan.is_a? Achoo::Timespan timespan = timeish_or_timespan return start <= timespan.start && self.end >= timespan.end else time = to_time(timeish_or_timespan) return start <= time && self.end >= time end end |
#overlaps?(timespan) ⇒ Boolean
46 47 48 49 50 |
# File 'lib/achoo/timespan.rb', line 46 def overlaps?(timespan) start <= timespan.start && self.end >= timespan.start \ || start <= timespan.end && self.end >= timespan.end \ || contains?(timespan) || timespan.contains?(self) end |
#to_s ⇒ Object
29 30 31 32 33 34 |
# File 'lib/achoo/timespan.rb', line 29 def to_s duration = duration_string from_to = from_to_string sprintf("(%s) %s", duration, from_to) end |