Class: Roxbury::WorkingHours
- Inherits:
-
Object
- Object
- Roxbury::WorkingHours
- Defined in:
- lib/roxbury/working_hours.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#begins_at ⇒ Object
Returns the value of attribute begins_at.
-
#ends_at ⇒ Object
Returns the value of attribute ends_at.
Class Method Summary collapse
Instance Method Summary collapse
- #at_beginning(timestamp) ⇒ Object
- #at_end(timestamp) ⇒ Object
- #ends_before?(timestamp) ⇒ Boolean
- #include?(timestamp) ⇒ Boolean
-
#initialize(begins_at:, ends_at:) ⇒ WorkingHours
constructor
A new instance of WorkingHours.
- #non_working? ⇒ Boolean
- #quantity(from: nil, to: nil) ⇒ Object
- #starts_after?(timestamp) ⇒ Boolean
- #working_day? ⇒ Boolean
Constructor Details
#initialize(begins_at:, ends_at:) ⇒ WorkingHours
Returns a new instance of WorkingHours.
20 21 22 |
# File 'lib/roxbury/working_hours.rb', line 20 def initialize begins_at:, ends_at: @begins_at, @ends_at = begins_at, ends_at end |
Instance Attribute Details
#begins_at ⇒ Object
Returns the value of attribute begins_at.
3 4 5 |
# File 'lib/roxbury/working_hours.rb', line 3 def begins_at @begins_at end |
#ends_at ⇒ Object
Returns the value of attribute ends_at.
3 4 5 |
# File 'lib/roxbury/working_hours.rb', line 3 def ends_at @ends_at end |
Class Method Details
.parse(bday_spec) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/roxbury/working_hours.rb', line 5 def self.parse bday_spec case bday_spec when Range if bday_spec.last <= 0 EmptyWorkingHours.new else new begins_at: bday_spec.first, ends_at: bday_spec.last end when nil EmptyWorkingHours.new else raise ArgumentError, "Business day spec not supported: #{bday_spec.inspect}" end end |
Instance Method Details
#at_beginning(timestamp) ⇒ Object
42 43 44 |
# File 'lib/roxbury/working_hours.rb', line 42 def at_beginning .change(hour: begins_at, min: 0, sec: 0) end |
#at_end(timestamp) ⇒ Object
46 47 48 |
# File 'lib/roxbury/working_hours.rb', line 46 def at_end .change(hour: [ends_at - 1, 0].max, min: 59, sec: 59) end |
#ends_before?(timestamp) ⇒ Boolean
38 39 40 |
# File 'lib/roxbury/working_hours.rb', line 38 def ends_before? > at_end() end |
#include?(timestamp) ⇒ Boolean
30 31 32 |
# File 'lib/roxbury/working_hours.rb', line 30 def include? (at_beginning()..at_end()).cover?() end |
#non_working? ⇒ Boolean
50 51 52 |
# File 'lib/roxbury/working_hours.rb', line 50 def non_working? false end |
#quantity(from: nil, to: nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/roxbury/working_hours.rb', line 24 def quantity from: nil, to: nil from = from ? hours_from_midnight(from) : begins_at to = to ? hours_from_midnight(to) : ends_at [[ends_at, to].min - [from, begins_at].max, 0].max end |
#starts_after?(timestamp) ⇒ Boolean
34 35 36 |
# File 'lib/roxbury/working_hours.rb', line 34 def starts_after? < at_beginning() end |
#working_day? ⇒ Boolean
54 55 56 |
# File 'lib/roxbury/working_hours.rb', line 54 def working_day? !non_working? end |