Class: Workpattern::Day
- Inherits:
-
Object
- Object
- Workpattern::Day
- Defined in:
- lib/workpattern/day.rb
Instance Attribute Summary collapse
-
#first_working_minute ⇒ Object
Returns the value of attribute first_working_minute.
-
#hours_per_day ⇒ Object
Returns the value of attribute hours_per_day.
-
#last_working_minute ⇒ Object
Returns the value of attribute last_working_minute.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Class Method Summary collapse
Instance Method Summary collapse
- #calc(a_date, a_duration) ⇒ Object
-
#initialize(hours_per_day = HOURS_IN_DAY, type = WORK_TYPE) ⇒ Day
constructor
A new instance of Day.
- #resting?(hour, minute) ⇒ Boolean
- #set_resting(start_time, finish_time) ⇒ Object
- #set_working(from_time, to_time) ⇒ Object
- #to_h ⇒ Object
- #working?(hour, minute) ⇒ Boolean
- #working_minutes(from_time = FIRST_TIME_IN_DAY, to_time = LAST_TIME_IN_DAY) ⇒ Object
Constructor Details
#initialize(hours_per_day = HOURS_IN_DAY, type = WORK_TYPE) ⇒ Day
Returns a new instance of Day.
30 31 32 33 34 |
# File 'lib/workpattern/day.rb', line 30 def initialize(hours_per_day = HOURS_IN_DAY, type = WORK_TYPE) @hours_per_day = hours_per_day @pattern = initial_day(type) set_first_and_last_minutes end |
Instance Attribute Details
#first_working_minute ⇒ Object
Returns the value of attribute first_working_minute.
5 6 7 |
# File 'lib/workpattern/day.rb', line 5 def first_working_minute @first_working_minute end |
#hours_per_day ⇒ Object
Returns the value of attribute hours_per_day.
5 6 7 |
# File 'lib/workpattern/day.rb', line 5 def hours_per_day @hours_per_day end |
#last_working_minute ⇒ Object
Returns the value of attribute last_working_minute.
5 6 7 |
# File 'lib/workpattern/day.rb', line 5 def last_working_minute @last_working_minute end |
#pattern ⇒ Object
Returns the value of attribute pattern.
6 7 8 |
# File 'lib/workpattern/day.rb', line 6 def pattern @pattern end |
Class Method Details
.from_h(h) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/workpattern/day.rb', line 17 def self.from_h(h) unless h[:hours_per_day].is_a?(Integer) && h[:hours_per_day] > 0 && h[:hours_per_day] <= HOURS_IN_DAY raise ArgumentError, "from_h: hours_per_day must be an Integer between 1 and #{HOURS_IN_DAY}" end unless h[:pattern].is_a?(String) && h[:pattern].length <= 400 raise ArgumentError, "from_h: pattern must be a hex String of at most 400 characters" end day = allocate day.hours_per_day = h[:hours_per_day] day.pattern = h[:pattern].to_i(16) day end |
Instance Method Details
#calc(a_date, a_duration) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/workpattern/day.rb', line 62 def calc(a_date, a_duration) if a_duration == 0 return a_date, a_duration, SAME_DAY else return a_duration > 0 ? add(a_date, a_duration) : subtract(a_date, a_duration) end end |
#resting?(hour, minute) ⇒ Boolean
58 59 60 |
# File 'lib/workpattern/day.rb', line 58 def resting?(hour, minute) !working?(hour,minute) end |
#set_resting(start_time, finish_time) ⇒ Object
36 37 38 39 40 |
# File 'lib/workpattern/day.rb', line 36 def set_resting(start_time, finish_time) mask = resting_mask(start_time, finish_time) @pattern = @pattern & mask set_first_and_last_minutes end |
#set_working(from_time, to_time) ⇒ Object
42 43 44 45 |
# File 'lib/workpattern/day.rb', line 42 def set_working(from_time, to_time) @pattern = @pattern | working_mask(from_time, to_time) set_first_and_last_minutes end |
#to_h ⇒ Object
13 14 15 |
# File 'lib/workpattern/day.rb', line 13 def to_h { pattern: @pattern.to_s(16), hours_per_day: @hours_per_day } end |
#working?(hour, minute) ⇒ Boolean
52 53 54 55 56 |
# File 'lib/workpattern/day.rb', line 52 def working?(hour, minute) mask = (2**((hour * 60) + minute)) result = mask & @pattern mask == result end |
#working_minutes(from_time = FIRST_TIME_IN_DAY, to_time = LAST_TIME_IN_DAY) ⇒ Object
47 48 49 50 |
# File 'lib/workpattern/day.rb', line 47 def working_minutes(from_time = FIRST_TIME_IN_DAY, to_time = LAST_TIME_IN_DAY) section = @pattern & working_mask(from_time, to_time) section.to_s(2).count('1') end |