Class: Achoo::Timespan

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/timespan.rb

Direct Known Subclasses

OpenTimespan

Constant Summary collapse

SECONDS_IN_A_DAY =
86400
SECONDS_IN_AN_HOUR =
3600
SECONDS_IN_A_MINUTE =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, end_) ⇒ Timespan

Returns a new instance of Timespan.

Raises:

  • (ArgumentError)


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

#endObject

Returns the value of attribute end.



12
13
14
# File 'lib/achoo/timespan.rb', line 12

def end
  @end
end

#startObject

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

Returns:

  • (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

Returns:

  • (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_sObject



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