Class: DateRange

Inherits:
Object
  • Object
show all
Defined in:
lib/date_range.rb,
lib/date_range/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date, end_date) ⇒ DateRange

Returns a new instance of DateRange.



8
9
10
11
# File 'lib/date_range.rb', line 8

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

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



6
7
8
# File 'lib/date_range.rb', line 6

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



5
6
7
# File 'lib/date_range.rb', line 5

def start_date
  @start_date
end

Instance Method Details

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/date_range.rb', line 13

def include?(other)
  if other.is_a?(DateRange)
    include?(other.start_date) && include?(other.end_date)
  else
    start_date <= other && end_date >= other
  end
end

#overlap(other) ⇒ Object



26
27
28
29
30
# File 'lib/date_range.rb', line 26

def overlap(other)
  return unless overlaps?(other)
  dates = [start_date, end_date, other.start_date, other.end_date].sort[1, 2]
  DateRange.new(*dates)
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps?(other)
  return unless other.is_a?(DateRange)
  other.include?(start_date) || other.include?(end_date) || include?(other)
end