class Workpattern::WeekPattern

Public Class Methods

new(work_pattern) click to toggle source
# File lib/workpattern/week_pattern.rb, line 3
def initialize(work_pattern)
  @work_pattern = work_pattern
end

Public Instance Methods

from() click to toggle source
# File lib/workpattern/week_pattern.rb, line 15
def from
  work_pattern.from
end
to() click to toggle source
# File lib/workpattern/week_pattern.rb, line 19
def to
  work_pattern.to
end
weeks() click to toggle source
# File lib/workpattern/week_pattern.rb, line 11
def weeks
  work_pattern.weeks
end
work_pattern() click to toggle source
# File lib/workpattern/week_pattern.rb, line 7
def work_pattern
  @work_pattern
end
workpattern(opts = {}, persist = nil) click to toggle source

Applys a working or resting pattern to the Workpattern object.

The resting and working methods are convenience methods that call this with the appropriate :work_type already set.

@param [Hash] opts the options used to apply a workpattern @option opts [Date] :start The first date to apply the pattern. Defaults

to the <tt>start</tt> attribute.

@option opts [Date] :finish The last date to apply the pattern. Defaults

to the <tt>finish</tt> attribute.

@option opts [DAYNAMES] :days The specific day or days the pattern will apply to.It defaults to :all @option opts [(hour, min)] :start_time The first time in the selected days to apply the pattern. Defaults to 00:00. @option opts [(hour, min)] :finish_time The last time in the selected days to apply the pattern. Defaults to 23:59. @option opts [(WORK_TYPE || REST_TYPE)] :work_type Either working or resting. Defaults to working. @see working @see resting

# File lib/workpattern/week_pattern.rb, line 43
def workpattern(opts = {}, persist = nil)
  args = all_workpattern_options(opts)

  persist.store(name: @name, workpattern: args) if !persist.nil?

  args = standardise_args(args)

  upd_start = work_pattern.to_utc(args[:start])
  upd_finish = work_pattern.to_utc(args[:finish])

  while upd_start <= upd_finish

          current_wp = work_pattern.find_weekpattern(upd_start)

    if current_wp.start == upd_start
      if current_wp.finish > upd_finish
        clone_wp = fetch_updatable_week_pattern(current_wp,
                                               upd_finish + DAY,
                                               current_wp.finish,
                                               upd_start,
                                               upd_finish)
        update_and_store_week_pattern(clone_wp, args)
        upd_start = upd_finish + DAY
      else # (current_wp.finish == upd_finish)
        current_wp.workpattern(args[:days], args[:from_time],
                               args[:to_time], args[:work_type])
        upd_start = current_wp.finish + DAY
      end
    else
      clone_wp = fetch_updatable_week_pattern(current_wp, current_wp.start,
                                             upd_start - DAY, upd_start)
      if clone_wp.finish > upd_finish
        after_wp = fetch_updatable_week_pattern(clone_wp,
                                               upd_start,
                                               upd_finish,
                                               upd_finish + DAY)
        weeks << after_wp
      end
      update_and_store_week_pattern(clone_wp, args)
      upd_start = clone_wp.finish + DAY
    end
  end
end

Private Instance Methods

adjust_date_range(week_pattern, start_date, finish_date) click to toggle source
# File lib/workpattern/week_pattern.rb, line 128
def adjust_date_range(week_pattern, start_date, finish_date)
  week_pattern.start = start_date
  week_pattern.finish = finish_date
end
all_workpattern_options(opts) click to toggle source
# File lib/workpattern/week_pattern.rb, line 89
def all_workpattern_options(opts)
        
  args = { start: from, finish: to, days: :all,
           from_time: FIRST_TIME_IN_DAY, to_time: LAST_TIME_IN_DAY,
           work_type: WORK_TYPE }

  args.merge! opts
end
dmy_date(date) click to toggle source

Strips off hours, minutes, seconds etc from a supplied Date or DateTime

@param [DateTime] date @return [DateTime] with zero hours, minutes, seconds and so forth.

# File lib/workpattern/week_pattern.rb, line 139
def dmy_date(date)
  Time.gm(date.year, date.month, date.day)
end
fetch_updatable_week_pattern(keep_week, keep_start, keep_finish, change_start, change_finish = nil) click to toggle source

Clones the supplied Week Pattern then changes the dates on it The newly cloned Week pattern dates are also changed and it is returned by this method

# File lib/workpattern/week_pattern.rb, line 110
def fetch_updatable_week_pattern(keep_week, keep_start, keep_finish,
                                change_start, change_finish = nil)
  change_week = keep_week.duplicate
  adjust_date_range(keep_week, keep_start, keep_finish)
  if change_finish.nil?
    adjust_date_range(change_week, change_start, change_week.finish)
  else
    adjust_date_range(change_week, change_start, change_finish)
  end
  change_week
end
standardise_args(args) click to toggle source
# File lib/workpattern/week_pattern.rb, line 98
def standardise_args(args)

  args[:start] = dmy_date(args[:start])
  args[:finish] = dmy_date(args[:finish])

  args
end
update_and_store_week_pattern(week_pattern, args) click to toggle source
# File lib/workpattern/week_pattern.rb, line 122
def update_and_store_week_pattern(week_pattern, args)
  week_pattern.workpattern(args[:days], args[:from_time],
                     args[:to_time], args[:work_type])
  weeks << week_pattern
end