class Repeatable::Schedule

Attributes

expression[R]

Public Class Methods

new(arg) click to toggle source
# File lib/repeatable/schedule.rb, line 3
def initialize(arg)
  case arg
  when Repeatable::Expression::Base
    @expression = arg
  when Hash
    @expression = Parser.call(arg)
  else
    fail(ParseError, "Can't build a Repeatable::Schedule from #{arg.class}")
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/repeatable/schedule.rb, line 45
def ==(other)
  other.is_a?(self.class) && expression == other.expression
end
include?(date = Date.today) click to toggle source
# File lib/repeatable/schedule.rb, line 36
def include?(date = Date.today)
  date = Conversions::Date(date)
  expression.include?(date)
end
next_occurrence(start_date = Date.today, include_start: false, limit: 36525) click to toggle source
# File lib/repeatable/schedule.rb, line 23
def next_occurrence(start_date = Date.today, include_start: false, limit: 36525)
  date = Conversions::Date(start_date)

  return date if include_start && include?(date)

  1.step do |i|
    date = date.next_day

    break date if include?(date)
    break if i == limit.to_i
  end
end
occurrences(start_date, end_date) click to toggle source
# File lib/repeatable/schedule.rb, line 14
def occurrences(start_date, end_date)
  start_date = Conversions::Date(start_date)
  end_date = Conversions::Date(end_date)

  fail(ArgumentError, "end_date must be equal to or after start_date") if end_date < start_date

  (start_date..end_date).select { |date| include?(date) }
end
to_h() click to toggle source
# File lib/repeatable/schedule.rb, line 41
def to_h
  expression.to_h
end