Class: TimeClock::Comparison

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

Defined Under Namespace

Classes: NilCalendarError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time, calendar = TimeClock.default_calendar) ⇒ Comparison

Returns a new instance of Comparison.

Raises:



8
9
10
11
12
13
# File 'lib/time_clock/comparison.rb', line 8

def initialize(start_time, end_time, calendar=TimeClock.default_calendar)
  raise NilCalendarError unless calendar
  @start_time = start_time.to_time
  @end_time = end_time_from_value(end_time)
  @calendar = calendar
end

Instance Attribute Details

#calendarObject (readonly)

Returns the value of attribute calendar.



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

def calendar
  @calendar
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Instance Method Details

#daysObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/time_clock/comparison.rb', line 25

def days
  dates = Set.new
  calendar.shifts.each do |shift|
    if period.overlapping_seconds(shift) > 0
      dates.add shift.start_time.to_date
      dates.add shift.end_time.to_date
    end
  end
  dates.size
end

#periodObject



15
16
17
# File 'lib/time_clock/comparison.rb', line 15

def period
  @period ||= Shift.new(earlier_time, later_time)
end

#secondsObject



19
20
21
22
23
# File 'lib/time_clock/comparison.rb', line 19

def seconds
  calendar.shifts.inject(0) do |total_seconds, shift|
    total_seconds + period.overlapping_seconds(shift)
  end
end