Module: Hiccup::Enumerable

Defined in:
lib/hiccup/enumerable.rb,
lib/hiccup/enumerable/never_enumerator.rb,
lib/hiccup/enumerable/weekly_enumerator.rb,
lib/hiccup/enumerable/monthly_enumerator.rb,
lib/hiccup/enumerable/annually_enumerator.rb,
lib/hiccup/enumerable/schedule_enumerator.rb,
lib/hiccup/enumerable/monthly_date_enumerator.rb

Defined Under Namespace

Classes: AnnuallyEnumerator, MonthlyDateEnumerator, MonthlyEnumerator, NeverEnumerator, ScheduleEnumerator, WeeklyEnumerator

Instance Method Summary collapse

Instance Method Details

#enumeratorObject



14
15
16
# File 'lib/hiccup/enumerable.rb', line 14

def enumerator
  ScheduleEnumerator.enum_for(self)
end

#first_n_occurrences(limit, options = {}) ⇒ Object



92
93
94
# File 'lib/hiccup/enumerable.rb', line 92

def first_n_occurrences(limit, options={})
  n_occurrences_on_or_after(limit, start_date, options)
end

#first_occurrence_after(date) ⇒ Object Also known as: next_occurrence_after



48
49
50
# File 'lib/hiccup/enumerable.rb', line 48

def first_occurrence_after(date)
  first_occurrence_on_or_after(date.to_date + 1)
end

#first_occurrence_before(date) ⇒ Object



59
60
61
# File 'lib/hiccup/enumerable.rb', line 59

def first_occurrence_before(date)
  first_occurrence_on_or_before(date.to_date - 1)
end

#first_occurrence_on_or_after(date) ⇒ Object



44
45
46
# File 'lib/hiccup/enumerable.rb', line 44

def first_occurrence_on_or_after(date)
  enumerator.new(self, date).next
end

#first_occurrence_on_or_before(date) ⇒ Object



55
56
57
# File 'lib/hiccup/enumerable.rb', line 55

def first_occurrence_on_or_before(date)
  enumerator.new(self, date).prev
end

#n_occurrences_after(limit, date, options = {}) ⇒ Object



96
97
98
# File 'lib/hiccup/enumerable.rb', line 96

def n_occurrences_after(limit, date, options={})
  n_occurrences_on_or_after(limit, date.to_date + 1, options)
end

#n_occurrences_before(limit, date, options = {}) ⇒ Object



76
77
78
# File 'lib/hiccup/enumerable.rb', line 76

def n_occurrences_before(limit, date, options={})
  n_occurrences_on_or_before(limit, date.to_date - 1, options)
end

#n_occurrences_on_or_after(limit, date, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/hiccup/enumerable.rb', line 100

def n_occurrences_on_or_after(limit, date, options={})
  exceptions = options.fetch(:except, [])
  occurrences = []
  enum = enumerator.new(self, date)
  while (occurrence = enum.next) && occurrences.length < limit
    occurrences << occurrence unless exceptions.member?(occurrence)
  end
  occurrences
end

#n_occurrences_on_or_before(limit, date, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/hiccup/enumerable.rb', line 80

def n_occurrences_on_or_before(limit, date, options={})
  exceptions = options.fetch(:except, [])
  occurrences = []
  enum = enumerator.new(self, date)
  while (occurrence = enum.prev) && occurrences.length < limit
    occurrences << occurrence unless exceptions.member?(occurrence)
  end
  occurrences
end

#occurrences_between(earlier_date, later_date) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/hiccup/enumerable.rb', line 33

def occurrences_between(earlier_date, later_date)
  occurrences = []
  enum = enumerator.new(self, earlier_date)
  while (occurrence = enum.next) && (occurrence <= later_date)
    occurrences << occurrence
  end
  occurrences
end

#occurs_on(date) ⇒ Object Also known as: contains?, includes?, member?, predicts?



65
66
67
68
# File 'lib/hiccup/enumerable.rb', line 65

def occurs_on(date)
  date = date.to_date
  date == first_occurrence_on_or_after(date)
end

#to_aObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/hiccup/enumerable.rb', line 20

def to_a
  raise UnboundedEnumerationError, "This schedule does not have an end date and so cannot be asked to list all of its dates, ever" unless ends?

  occurrences = []
  enum = enumerator.new(self, start_date)
  while occurrence = enum.next
    occurrences << occurrence
  end
  occurrences
end