Class: Workhours::Period
- Inherits:
-
Object
- Object
- Workhours::Period
- Defined in:
- lib/workhours/period.rb
Instance Attribute Summary collapse
-
#shift ⇒ Object
Returns the value of attribute shift.
-
#wday ⇒ Object
Returns the value of attribute wday.
Instance Method Summary collapse
- #beginning ⇒ Object
- #ending ⇒ Object
- #ending_time(time) ⇒ Object
-
#initialize(wday, beginning, ending) ⇒ Period
constructor
A new instance of Period.
- #inspect ⇒ Object
- #is_active?(time) ⇒ Boolean
- #is_inside_range?(tod) ⇒ Boolean
- #is_today?(time) ⇒ Boolean
- #is_tomorrow?(time) ⇒ Boolean
- #is_yesterday?(time) ⇒ Boolean
- #overlaps?(other_day) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(wday, beginning, ending) ⇒ Period
Returns a new instance of Period.
4 5 6 7 8 9 10 11 12 |
# File 'lib/workhours/period.rb', line 4 def initialize(wday, beginning, ending) @wday = wday beginning = TimeOfDay.parse(beginning) unless beginning.is_a?(TimeOfDay) ending = TimeOfDay.parse(ending) unless ending.is_a?(TimeOfDay) if ending.second_of_day != 0 && ending < beginning raise MultidayPeriodError.new() end @shift = Shift.new(beginning, ending) end |
Instance Attribute Details
#shift ⇒ Object
Returns the value of attribute shift.
3 4 5 |
# File 'lib/workhours/period.rb', line 3 def shift @shift end |
#wday ⇒ Object
Returns the value of attribute wday.
3 4 5 |
# File 'lib/workhours/period.rb', line 3 def wday @wday end |
Instance Method Details
#beginning ⇒ Object
32 33 34 |
# File 'lib/workhours/period.rb', line 32 def beginning shift.beginning end |
#ending ⇒ Object
35 36 37 |
# File 'lib/workhours/period.rb', line 35 def ending shift.ending end |
#ending_time(time) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/workhours/period.rb', line 39 def ending_time(time) if shift.ending.second_of_day == 0 (time.to_date + 1).at(shift.ending) else time.to_date.at(shift.ending) end end |
#inspect ⇒ Object
14 15 16 |
# File 'lib/workhours/period.rb', line 14 def inspect "<Workhours::Period wday:#{wday} beginning:#{beginning.to_s} ending:#{ending.to_s}>" end |
#is_active?(time) ⇒ Boolean
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/workhours/period.rb', line 47 def is_active?(time) tod = time.to_time_of_day if is_today?(time) if tod.second_of_day == 0 && ending.second_of_day == 0 && beginning.second_of_day > 0 # 10:00-0:00 is NOT active on 0:00 of current day, but on 0:00 of next day false else is_inside_range?(tod) end else if tod.second_of_day == 0 && ending.second_of_day == 0 && is_tomorrow?(time) # 10:00-0:00 is active on 0:00 of next day true else false end end end |
#is_inside_range?(tod) ⇒ Boolean
18 19 20 |
# File 'lib/workhours/period.rb', line 18 def is_inside_range?(tod) shift.include?(tod) end |
#is_today?(time) ⇒ Boolean
22 23 24 |
# File 'lib/workhours/period.rb', line 22 def is_today?(time) Workhours.is_today?(wday, time) end |
#is_tomorrow?(time) ⇒ Boolean
25 26 27 |
# File 'lib/workhours/period.rb', line 25 def is_tomorrow?(time) Workhours.is_tomorrow?(wday, time) end |
#is_yesterday?(time) ⇒ Boolean
28 29 30 |
# File 'lib/workhours/period.rb', line 28 def is_yesterday?(time) Workhours.is_yesterday?(wday, time) end |
#overlaps?(other_day) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/workhours/period.rb', line 70 def overlaps?(other_day) if wday != other_day.wday return false end open_inside = beginning > other_day.beginning && beginning < other_day.ending close_inside = ending > other_day.beginning && ending < other_day.ending outside = beginning < other_day.beginning && ending > other_day.ending #puts "#{to_s} vs #{other_day.to_s}: oi:#{open_inside} ci:#{close_inside} ou:#{outside}" open_inside || close_inside || outside end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/workhours/period.rb', line 66 def to_s "#{wday} #{beginning.to_s}-#{ending.to_s}" end |