class Roxbury::WorkingHours

Attributes

begins_at[RW]
ends_at[RW]

Public Class Methods

new(begins_at:, ends_at: @begins_at, @ends_at = begins_at, ends_at) click to toggle source
# File lib/roxbury/working_hours.rb, line 20
def initialize begins_at:, ends_at:
  @begins_at, @ends_at = begins_at, ends_at
end
parse(bday_spec) click to toggle source
# 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

Public Instance Methods

at_beginning(timestamp) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/roxbury/working_hours.rb, line 38
def ends_before? timestamp
  timestamp > at_end(timestamp)
end
include?(timestamp) click to toggle source
# File lib/roxbury/working_hours.rb, line 30
def include? timestamp
  (at_beginning(timestamp)..at_end(timestamp)).cover?(timestamp)
end
non_working?() click to toggle source
# File lib/roxbury/working_hours.rb, line 50
def non_working?
  false
end
quantity(from: nil, to: nil) click to toggle source
# 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) click to toggle source
# File lib/roxbury/working_hours.rb, line 34
def starts_after? timestamp
  timestamp < at_beginning(timestamp)
end
working_day?() click to toggle source
# File lib/roxbury/working_hours.rb, line 54
def working_day?
  !non_working?
end

Private Instance Methods

hours_from_midnight(time) click to toggle source
# File lib/roxbury/working_hours.rb, line 60
def hours_from_midnight time
  time.seconds_since_midnight / 1.0.hour
end