Sha256: 06e4ce259ea4650ea922c3c684b92d4750f77e0191ae28824e1c3df9394d2f5b

Contents?: true

Size: 769 Bytes

Versions: 2

Compression:

Stored size: 769 Bytes

Contents

module TimeClock
  class Comparison

    class NilCalendarError < ArgumentError; end

    attr_reader :start_time, :end_time, :calendar

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

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

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

  private

    def earlier_time
      start_time < end_time ? start_time : end_time
    end

    def later_time
      end_time > start_time ? end_time : start_time
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
time_clock-0.0.5 lib/time_clock/comparison.rb
time_clock-0.0.4 lib/time_clock/comparison.rb