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

Class Method 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.



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

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 + 1)
  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

Class Method Details

.from_h(h) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/workpattern/week.rb', line 29

def self.from_h(h)
  s = h[:start]
  f = h[:finish]
  week = allocate
  week.hours_per_day = HOURS_IN_DAY
  week.start  = Time.gm(s[:year], s[:month], s[:day])
  week.finish = Time.gm(f[:year], f[:month], f[:day])
  week.days   = Array.new(LAST_DAY_OF_WEEK + 1)
  h[:days].each_with_index { |dh, i| week.days[i] = Day.from_h(dh) }
  week
end

Instance Method Details

#<=>(other) ⇒ Object



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

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



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

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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/workpattern/week.rb', line 93

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



65
66
67
68
69
70
71
72
73
# File 'lib/workpattern/week.rb', line 65

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)


89
90
91
# File 'lib/workpattern/week.rb', line 89

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

#to_hObject



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

def to_h
  { start:  { year: @start.year,  month: @start.month,  day: @start.day },
    finish: { year: @finish.year, month: @finish.month, day: @finish.day },
    days:   (FIRST_DAY_OF_WEEK..LAST_DAY_OF_WEEK).map { |i| @days[i].to_h } }
end

#totalObject



51
52
53
# File 'lib/workpattern/week.rb', line 51

def total
  elapsed_days < 8 ? week_total : range_total
end

#week_totalObject



47
48
49
# File 'lib/workpattern/week.rb', line 47

def week_total
  elapsed_days > 6 ? full_week_working_minutes : part_week_total_minutes
end

#working?(time) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/workpattern/week.rb', line 85

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

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



55
56
57
58
59
60
61
62
63
# File 'lib/workpattern/week.rb', line 55

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