Class: Workpattern::Week
- Inherits:
-
Object
- Object
- Workpattern::Week
- 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
-
#days ⇒ Object
Returns the value of attribute days.
-
#finish ⇒ Object
Returns the value of attribute finish.
-
#hours_per_day ⇒ Object
Returns the value of attribute hours_per_day.
-
#start ⇒ Object
Returns the value of attribute start.
- #total ⇒ Object
- #week_total ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #calc(a_date, a_duration, a_day = SAME_DAY) ⇒ Object
- #diff(start_date, finish_date) ⇒ Object
- #duplicate ⇒ Object
-
#initialize(start, finish, type = WORK_TYPE, hours_per_day = HOURS_IN_DAY) ⇒ Week
constructor
A new instance of Week.
- #resting?(time) ⇒ Boolean
- #working?(time) ⇒ Boolean
- #workpattern(days, from_time, to_time, type) ⇒ Object
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
#days ⇒ Object
Returns the value of attribute days.
11 12 13 |
# File 'lib/workpattern/week.rb', line 11 def days @days end |
#finish ⇒ Object
Returns the value of attribute finish.
11 12 13 |
# File 'lib/workpattern/week.rb', line 11 def finish @finish end |
#hours_per_day ⇒ Object
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 |
#start ⇒ Object
Returns the value of attribute start.
11 12 13 |
# File 'lib/workpattern/week.rb', line 11 def start @start end |
#total ⇒ Object
34 35 36 |
# File 'lib/workpattern/week.rb', line 34 def total elapsed_days < 8 ? week_total : range_total end |
#week_total ⇒ Object
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 |
#duplicate ⇒ Object
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
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
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 |