Class: Workpattern::Week

Inherits:
Object
  • Object
show all
Defined in:
lib/workpattern/week.rb

Overview

The representation of a week might not be obvious so I am writing about it here. It will also help me if I ever need to come back to this in the future.

Each day is represented by a binary number where a 1 represents a working minute and a 0 represents a resting minute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, finish, type = WORK_TYPE, hours_per_day = HOURS_IN_DAY) ⇒ Week

Returns a new instance of Week.



14
15
16
17
18
19
20
21
22
# File 'lib/workpattern/week.rb', line 14

def initialize(start, finish, type = WORK_TYPE, hours_per_day = HOURS_IN_DAY)
  @hours_per_day = hours_per_day
  @start = Time.gm(start.year, start.month, start.day)
  @finish = Time.gm(finish.year, finish.month, finish.day)
  @days = Array.new(LAST_DAY_OF_WEEK)
  FIRST_DAY_OF_WEEK.upto(LAST_DAY_OF_WEEK) do |i|
    @days[i] = Day.new(hours_per_day, type)
  end
end

Instance Attribute Details

#daysObject

Returns the value of attribute days.



11
12
13
# File 'lib/workpattern/week.rb', line 11

def days
  @days
end

#finishObject

Returns the value of attribute finish.



11
12
13
# File 'lib/workpattern/week.rb', line 11

def finish
  @finish
end

#hours_per_dayObject

Returns the value of attribute hours_per_day.



11
12
13
# File 'lib/workpattern/week.rb', line 11

def hours_per_day
  @hours_per_day
end

#startObject

Returns the value of attribute start.



11
12
13
# File 'lib/workpattern/week.rb', line 11

def start
  @start
end

#totalObject



34
35
36
# File 'lib/workpattern/week.rb', line 34

def total
  elapsed_days < 8 ? week_total : range_total
end

#week_totalObject



30
31
32
# File 'lib/workpattern/week.rb', line 30

def week_total
  elapsed_days > 6 ? full_week_working_minutes : part_week_total_minutes
end

Instance Method Details

#<=>(other) ⇒ Object



24
25
26
27
28
# File 'lib/workpattern/week.rb', line 24

def <=>(other)
  return -1 if start < other.start
  return 0 if start == other.start
  1
end

#calc(a_date, a_duration, a_day = SAME_DAY) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/workpattern/week.rb', line 58

def calc(a_date, a_duration, a_day = SAME_DAY) 
  if a_duration == 0
    return a_date, a_duration
  elsif a_duration > 0	
    return add(a_date, a_duration)
  else	
    subtract(a_date, a_duration, a_day)
  end	
end

#diff(start_date, finish_date) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/workpattern/week.rb', line 76

def diff(start_date, finish_date)
  if start_date > finish_date
    start_date, finish_date = finish_date, start_date
  end

  if jd(start_date) == jd(finish_date)
    return diff_in_same_day(start_date, finish_date)
  else
    return diff_in_same_weekpattern(start_date, finish_date)
  end
end

#duplicateObject



48
49
50
51
52
53
54
55
56
# File 'lib/workpattern/week.rb', line 48

def duplicate
  duplicate_week = Week.new(@start, @finish)
  FIRST_DAY_OF_WEEK.upto(LAST_DAY_OF_WEEK) do |i|
    duplicate_week.days[i] = @days[i].clone
   duplicate_week.days[i].hours_per_day = @days[i].hours_per_day
   duplicate_week.days[i].pattern = @days[i].pattern
  end
  duplicate_week
end

#resting?(time) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/workpattern/week.rb', line 72

def resting?(time)
  @days[time.wday].resting?(time.hour, time.min)
end

#working?(time) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/workpattern/week.rb', line 68

def working?(time)
  @days[time.wday].working?(time.hour, time.min)
end

#workpattern(days, from_time, to_time, type) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/workpattern/week.rb', line 38

def workpattern(days, from_time, to_time, type)
  DAYNAMES[days].each do |day|
    if type == WORK_TYPE
      @days[day].set_working(from_time, to_time)
    else
      @days[day].set_resting(from_time, to_time)
    end
  end
end