Class: Workpattern::Day

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hours_per_day = HOURS_IN_DAY, type = WORK_TYPE) ⇒ Day

Returns a new instance of Day.



7
8
9
10
11
# File 'lib/workpattern/day.rb', line 7

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_minuteObject

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_dayObject

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_minuteObject

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

#patternObject

Returns the value of attribute pattern.



5
6
7
# File 'lib/workpattern/day.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#calc(a_date, a_duration) ⇒ Object



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

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/workpattern/day.rb', line 35

def resting?(hour, minute)
  !working?(hour,minute)
end

#set_resting(start_time, finish_time) ⇒ Object



13
14
15
16
17
# File 'lib/workpattern/day.rb', line 13

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



19
20
21
22
# File 'lib/workpattern/day.rb', line 19

def set_working(from_time, to_time)
  @pattern = @pattern | working_mask(from_time, to_time)
  set_first_and_last_minutes
end

#working?(hour, minute) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/workpattern/day.rb', line 29

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



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

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