Sha256: ec2f54cead174b14bbb947af4b75bbf294a68e2a02b49712f3624265b96df27f

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

require 'gb_work_day/work_week'
require 'gb_work_day/duration'
module GBWorkDay
  class Interval
    attr_reader :start_time, :end_time

    # @param start_time [Time, Date]
    # @param end_time [Time, Date]
    # @param params [Hash]
    # @option params [Fixnum] :work_days number of work days in a week, default is 7
    # @option params [Fixnum] :week_start first working day in a week as number. Default value is 1 corresponding to Monday.
    def initialize(start_time, end_time, params=Hash.new)
      @start_time = start_time
      @end_time = end_time
      @symbol = 1
      revert if @start_time > @end_time
      @working_week = params[:week]
      unless @working_week
        if params[:work_days] || params[:week_start]
          work_days = params.fetch(:work_days, 7)
          week_start = params.fetch(:week_start, 1)
          @working_week = WorkWeek.new work_days, week_start
        else
          @working_week = WorkWeek.current
        end
      end
    end

    # @return [Integer] Number of working days in a given period
    def work_days
      date = @start_time
      work_days = 0
      while date < end_time
        work_days += 1 if @working_week.work_day?(date)
        date += 1.day
      end
      Duration.new(work_days * @symbol, @working_week)
    end

    alias_method :duration, :work_days

    def endpoints
      [start_time, end_time]
    end

    private

    def revert
      new_end_time = @start_time
      @start_time = @end_time
      @end_time = new_end_time
      @symbol= @symbol * -1
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gb_work_day-0.0.6 lib/gb_work_day/interval.rb
gb_work_day-0.0.4 lib/gb_work_day/interval.rb
gb_work_day-0.0.3 lib/gb_work_day/interval.rb
gb_work_day-0.0.2 lib/gb_work_day/interval.rb
gb_work_day-0.0.1 lib/gb_work_day/interval.rb