class Blackcal::Schedule

Represents a schedule

Attributes

days[R]

@return [DayRange] days when this schedule is active

months[R]

@return [MonthRange] months when this schedule is active

time_of_day[R]

@return [TimeOfDay] time of day when this schedule is active

time_range[R]

@return [TimeRange] the time the schedule is active

weekdays[R]

@return [WeekdayRange] weekdays when this schedule is active

weeks_of_month[R]

@return [WeeksOfMonthRange] weeks of month when this schedule is active

Public Class Methods

new( start_time: nil, finish_time: nil, start_time_of_day: nil, finish_hour_of_day: nil, months: nil, weekdays: nil, weeks_of_month: nil, days: nil ) click to toggle source

Initialize schedule @param start_time [Time, Date, String, nil] @param finish_time [Time, Date, String, nil] @param start_time_of_day [TimeOfDay, Time, Integer, nil] @param finish_hour_of_day [TimeOfDay, Time, Integer, nil] @param months [Array<String>, Array<Symbol>, String, Symbol, nil] @param weekdays [Array<String>, Array<Symbol>, String, Symbol, nil] @param weeks_of_month [Array<Integer>, nil] @param days [Array<Integer>, Integer, nil] @see TimeRange#initialize @see TimeOfDayRange#initialize @see MonthRange#initialize @see WeekdayRange#initialize @see DayRange#initialize

# File lib/blackcal/schedule.rb, line 48
def initialize(
  start_time: nil,
  finish_time: nil,
  start_time_of_day: nil,
  finish_hour_of_day: nil,
  months: nil,
  weekdays: nil,
  weeks_of_month: nil,
  days: nil
)
  if start_time || finish_time
    @time_range = TimeRange.new(parse_time(start_time), parse_time(finish_time))
  end

  if start_time_of_day || finish_hour_of_day
    @time_of_day = TimeOfDayRange.new(start_time_of_day, finish_hour_of_day)
  end

  if months
    @months = MonthRange.new(months)
  end

  if weeks_of_month
    @weeks_of_month = WeeksOfMonthRange.new(weeks_of_month)
  end

  if weekdays
    @weekdays = WeekdayRange.new(weekdays)
  end

  if days
    @days = DayRange.new(days)
  end
end

Public Instance Methods

cover?(timestamp) click to toggle source

Returns true if calendar is open for timestamp @param [Time] timestamp @return [Boolean]

# File lib/blackcal/schedule.rb, line 86
def cover?(timestamp)
  timestamp = parse_time(timestamp)
  return false if @time_range && !@time_range.cover?(timestamp)

  ranges = [@months, @weekdays, @days, @time_of_day, @weeks_of_month].compact
  return false if ranges.empty?

  ranges.all? { |range| range.cover?(timestamp) }
end
to_matrix(start_date:, finish_date:, resolution: :hour) click to toggle source

Returns schedule represented as a matrix @param start_date [Date] @param finish_date [Date] @return [Array<Array<Boolean>>]

# File lib/blackcal/schedule.rb, line 100
def to_matrix(start_date:, finish_date:, resolution: :hour)
  start_time = parse_time(start_date).to_time
  finish_time = parse_time(finish_date).to_time
  slots = resolution == :hour ? 24 : 24 * 60
  matrix = SlotMatrix.new(slots)

  # TODO: This is needlessly inefficient..
  time_range = TimeRange.new(start_time, finish_time)
  time_range.each(resolution: resolution) do |time|
    matrix << cover?(time)
  end

  matrix.data
end

Private Instance Methods

parse_time(time) click to toggle source
# File lib/blackcal/schedule.rb, line 117
def parse_time(time)
  return Time.parse(time) if time.is_a?(String)

  time
end