class Biz::Configuration

Constants

Raw

Attributes

raw[R]

Public Class Methods

new() { |raw| ... } click to toggle source
# File lib/biz/configuration.rb, line 6
def initialize
  @raw = Raw.new

  yield raw if block_given?

  raw.freeze

  Validation.perform(self)
end

Public Instance Methods

&(other) click to toggle source
# File lib/biz/configuration.rb, line 70
def &(other)
  self.class.new do |config|
    config.hours     = Interval.to_hours(intersected_intervals(other))
    config.breaks    = combined_breaks(other)
    config.holidays  = [*raw.holidays, *other.raw.holidays].map(&:to_date)
    config.time_zone = raw.time_zone
  end
end
breaks() click to toggle source
# File lib/biz/configuration.rb, line 38
def breaks
  @breaks ||= begin
    raw
      .breaks
      .flat_map { |date, hours|
        hours.map { |timestamps| date_period(date, timestamps) }
      }
      .sort
      .freeze
  end
end
holidays() click to toggle source
# File lib/biz/configuration.rb, line 50
def holidays
  @holidays ||= begin
    raw
      .holidays
      .to_a
      .uniq
      .map { |date| Holiday.new(date, time_zone) }
      .sort
      .freeze
  end
end
intervals() click to toggle source
# File lib/biz/configuration.rb, line 16
def intervals
  @intervals ||= begin
    raw
      .hours
      .flat_map { |weekday, hours| weekday_intervals(weekday, hours) }
      .sort
      .freeze
  end
end
shifts() click to toggle source
# File lib/biz/configuration.rb, line 26
def shifts
  @shifts ||= begin
    raw
      .shifts
      .flat_map { |date, hours|
        hours.map { |timestamps| date_period(date, timestamps) }
      }
      .sort
      .freeze
  end
end
time_zone() click to toggle source
# File lib/biz/configuration.rb, line 62
def time_zone
  @time_zone ||= TZInfo::TimezoneProxy.new(raw.time_zone)
end
weekdays() click to toggle source
# File lib/biz/configuration.rb, line 66
def weekdays
  @weekdays ||= raw.hours.keys.uniq.freeze
end

Private Instance Methods

combined_breaks(other) click to toggle source
# File lib/biz/configuration.rb, line 131
def combined_breaks(other)
  Hash.new do |config, date| config.store(date, {}) end.tap do |combined|
    [raw.breaks, other.raw.breaks].each do |configured|
      configured.each do |date, breaks| combined[date].merge!(breaks) end
    end
  end
end
date_period(date, timestamps) click to toggle source
# File lib/biz/configuration.rb, line 115
def date_period(date, timestamps)
  TimeSegment.new(
    time.on_date(date, DayTime.from_timestamp(timestamps.first)),
    time.on_date(date, DayTime.from_timestamp(timestamps.last))
  )
end
intersected_intervals(other) click to toggle source
# File lib/biz/configuration.rb, line 122
def intersected_intervals(other)
  intervals.flat_map { |interval|
    other
      .intervals
      .map { |other_interval| interval & other_interval }
      .reject(&:empty?)
  }
end
time() click to toggle source
# File lib/biz/configuration.rb, line 95
def time
  @time ||= Time.new(time_zone)
end
to_proc() click to toggle source
# File lib/biz/configuration.rb, line 85
def to_proc
  proc do |config|
    config.hours     = raw.hours
    config.shifts    = raw.shifts
    config.breaks    = raw.breaks
    config.holidays  = raw.holidays
    config.time_zone = raw.time_zone
  end
end
weekday_intervals(weekday, hours) click to toggle source
# File lib/biz/configuration.rb, line 99
def weekday_intervals(weekday, hours)
  hours.map { |start_timestamp, end_timestamp|
    Interval.new(
      WeekTime.start(
        DayOfWeek.from_symbol(weekday).start_minute +
          DayTime.from_timestamp(start_timestamp).day_minute
      ),
      WeekTime.end(
        DayOfWeek.from_symbol(weekday).start_minute +
          DayTime.from_timestamp(end_timestamp).day_minute
      ),
      time_zone
    )
  }
end