Class: TimeClock::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/time_clock/calendar.rb

Defined Under Namespace

Classes: OverlappingShiftError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCalendar

Returns a new instance of Calendar.



8
9
10
# File 'lib/time_clock/calendar.rb', line 8

def initialize
  @shifts = []
end

Instance Attribute Details

#shiftsObject (readonly)

Returns the value of attribute shifts.



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

def shifts
  @shifts
end

Instance Method Details

#add_shift(new_shift) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/time_clock/calendar.rb', line 12

def add_shift(new_shift)
  starting_shift_count = @shifts.size
  shifts.each_with_index do |shift, index|
    raise OverlappingShiftError if new_shift.overlaps?(shift)
    break shifts.insert(index, new_shift) if shift.start_time > new_shift.end_time
  end
  shifts << new_shift unless shifts.size > starting_shift_count
  shifts
end