Class: Roxbury::WorkingHours

Inherits:
Object
  • Object
show all
Defined in:
lib/roxbury/working_hours.rb

Direct Known Subclasses

EmptyWorkingHours

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject

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_atObject

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 timestamp
  timestamp.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 timestamp
  timestamp.change(hour: [ends_at - 1, 0].max, min: 59, sec: 59)
end

#ends_before?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/roxbury/working_hours.rb', line 38

def ends_before? timestamp
  timestamp > at_end(timestamp)
end

#include?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/roxbury/working_hours.rb', line 30

def include? timestamp
  (at_beginning(timestamp)..at_end(timestamp)).cover?(timestamp)
end

#non_working?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


34
35
36
# File 'lib/roxbury/working_hours.rb', line 34

def starts_after? timestamp
  timestamp < at_beginning(timestamp)
end

#working_day?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/roxbury/working_hours.rb', line 54

def working_day?
  !non_working?
end