Class: Tempora::TimePeriod

Inherits:
Object
  • Object
show all
Includes:
Comparable, HasDays, HasTime
Defined in:
lib/timeperiod.rb

Direct Known Subclasses

Month, Quarter, Week, Year

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasTime

#end_time, #start_time

Methods included from HasDays

#days, #each_day

Constructor Details

#initialize(start_date, end_date) ⇒ TimePeriod

Returns a new instance of TimePeriod.



13
14
15
16
# File 'lib/timeperiod.rb', line 13

def initialize(start_date, end_date)
  @start_date = start_date
  @end_date   = end_date
end

Instance Attribute Details

#end_dateObject (readonly) Also known as: end, last_day

Returns the value of attribute end_date.



11
12
13
# File 'lib/timeperiod.rb', line 11

def end_date
  @end_date
end

#start_dateObject (readonly) Also known as: begin, first_day

Returns the value of attribute start_date.



11
12
13
# File 'lib/timeperiod.rb', line 11

def start_date
  @start_date
end

Instance Method Details

#<=>(other) ⇒ Object



47
48
49
# File 'lib/timeperiod.rb', line 47

def <=>(other)
  start_date <=> other.start_date
end

#contains?(date) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/timeperiod.rb', line 26

def contains?(date)
  range.cover?(date)
end

#durationObject Also known as: length



22
23
24
# File 'lib/timeperiod.rb', line 22

def duration
  (end_date - start_date).to_i + 1
end

#hashObject



51
52
53
# File 'lib/timeperiod.rb', line 51

def hash
  [self.class, id || range].hash
end

#intersection(other) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/timeperiod.rb', line 34

def intersection(other)
  return nil unless overlaps?(other)

  new_start = [start_date, other.start_date].max
  new_end   = [end_date, other.end_date].min

  TimePeriod.new(new_start, new_end)
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/timeperiod.rb', line 30

def overlaps?(other)
  range.overlap?(other.range)
end

#rangeObject



18
19
20
# File 'lib/timeperiod.rb', line 18

def range
  start_date..end_date
end

#shift(days) ⇒ Object



43
44
45
# File 'lib/timeperiod.rb', line 43

def shift(days)
  TimePeriod.new(@start_date + days, @end_date + days)
end