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 = 1, hours_per_day = 24) ⇒ 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 = 1, hours_per_day = 24)
  @hours_per_day = hours_per_day
  @start = Time.gm(start.year, start.month, start.day)
  @finish = Time.gm(finish.year, finish.month, finish.day)
  @values = Array.new(6)
  0.upto(6) do |i|
    @values[i] = working_day * type
  end
end

Instance Attribute Details

#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

Returns the value of attribute total.



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

def total
  @total
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

#week_totalObject

Returns the value of attribute week_total.



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

def week_total
  @week_total
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#calc(start_date, duration, midnight = false) ⇒ Object



53
54
55
56
57
58
# File 'lib/workpattern/week.rb', line 53

def calc(start_date, duration, midnight = false)
  return start_date, duration, false if duration == 0
  return add(start_date, duration) if duration > 0
  return subtract(start, duration, midnight) if total == 0 && duration < 0
  subtract(start_date, duration, midnight)
end

#diff(start_d, finish_d) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/workpattern/week.rb', line 69

def diff(start_d, finish_d)
  start_d, finish_d = finish_d, start_d if ((start_d <=> finish_d)) == 1

  return diff_in_same_day(start_d, finish_d) if jd(start_d) == jd(finish_d)
  return diff_in_same_weekpattern(start_d, finish_d) if jd(finish_d) <= jd(finish)
  diff_beyond_weekpattern(start_d, finish_d)
end

#duplicateObject



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

def duplicate
  duplicate_week = Week.new(start, finish)
  0.upto(6).each { |i| duplicate_week.values[i] = @values[i] }
  duplicate_week
end

#resting?(date) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/workpattern/week.rb', line 65

def resting?(date)
  !working?(date)
end

#working?(time) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/workpattern/week.rb', line 60

def working?(time)
  return true if bit_pos(time.hour, time.min) & @values[time.wday] > 0
  false
end

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



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

def workpattern(days, from_time, to_time, type)
  DAYNAMES[days].each do |day|
    if type == 1
      work_on_day(day, from_time, to_time)
    else
      rest_on_day(day, from_time, to_time)
    end
  end
end