Class: Repeatable::Schedule

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/repeatable/schedule.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Schedule

Returns a new instance of Schedule.



8
9
10
11
12
13
14
15
16
17
# File 'lib/repeatable/schedule.rb', line 8

def initialize(arg)
  case arg
  when Repeatable::Expression::Base
    @expression = T.let(arg, Expression::Base)
  when Hash
    @expression = Parser.call(arg)
  else
    fail(ParseError, "Can't build a Repeatable::Schedule from #{arg.class}")
  end
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/repeatable/schedule.rb', line 64

def ==(other)
  other.is_a?(self.class) && expression == other.expression
end

#deconstruct_keys(_keys) ⇒ Object



59
60
61
# File 'lib/repeatable/schedule.rb', line 59

def deconstruct_keys(_keys)
  to_h
end

#include?(date = Date.today) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/repeatable/schedule.rb', line 48

def include?(date = Date.today)
  date = Conversions::Date(date)
  expression.include?(date)
end

#next_occurrence(start_date = Date.today, include_start: false, limit: 36525) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/repeatable/schedule.rb', line 30

def next_occurrence(start_date = Date.today, include_start: false, limit: 36525)
  date = Conversions::Date(start_date)

  return date if include_start && include?(date)

  result = T.let(nil, T.nilable(Date))
  1.step(limit) do |i|
    date = date.next_day

    if include?(date)
      result = date
      break
    end
  end
  result
end

#occurrences(start_date, end_date) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/repeatable/schedule.rb', line 20

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_hObject



54
55
56
# File 'lib/repeatable/schedule.rb', line 54

def to_h
  expression.to_h
end