Class: RecurringDate::Enumerator

Inherits:
Enumerator
  • Object
show all
Defined in:
lib/recurring_date/enumerator.rb

Overview

The enumerator over the dates.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ Enumerator

Initiate the enumerator.



9
10
11
12
13
14
15
# File 'lib/recurring_date/enumerator.rb', line 9

def initialize(range)
  super() do |result|
    range.each { |val| block_given? ? yield(result, val) : result << val }
  rescue ::StopIteration
    nil
  end
end

Class Method Details

.eternityObject

Returns a lazy, perpetual enumerator over dates, day by day, from 1st January 1970.



18
19
20
# File 'lib/recurring_date/enumerator.rb', line 18

def self.eternity
  from(Date.new(1970))
end

.from(date) ⇒ Object

Returns a lazy, perpetual enumerator over dates, day by day, from date.

Parameters:

Raises:

  • (::ArgumentError)


24
25
26
27
28
# File 'lib/recurring_date/enumerator.rb', line 24

def self.from(date)
  raise ::ArgumentError, 'not a date' unless date.is_a?(::Date)

  RecurringDate::Enumerator.new(infinity(date))
end

.infinity(date) ⇒ Object

The actual enumerator. The infinity method returns an Enumerator that iterates over a dates, day by day, starting from date.

Parameters:

  • date (Date)

    starting date

Raises:

  • (::ArgumentError)


204
205
206
207
208
209
210
211
# File 'lib/recurring_date/enumerator.rb', line 204

def self.infinity(date)
  raise ::ArgumentError, 'not a date' unless date.is_a?(::Date)

  ::Enumerator.new do |y|
    i = date.prev_day
    loop { y << (i = i.next) }
  end
end

Instance Method Details

#inspectObject

Dummy inspect for enumerator.



197
198
199
# File 'lib/recurring_date/enumerator.rb', line 197

def inspect
  ['#<RecurringDate::Enumerator:0x', object_id, '>'].join
end

#matching(*args) ⇒ Object

Filter dates that matches args with yielded block.

Parameters:

  • args (Array)

    arguments that are compared to the expession in given block.



128
129
130
# File 'lib/recurring_date/enumerator.rb', line 128

def matching(*args)
  select { |date| args.include?(yield(date)) }
end

#mday(*args) ⇒ Object

Filter dates by a mday of a date.

Parameters:

  • args (Array)

    mdays

Raises:

  • (::ArgumentError)


40
41
42
43
44
# File 'lib/recurring_date/enumerator.rb', line 40

def mday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:mday)
end

#month(*args) ⇒ Object

Filter dates by a month of a date.

Parameters:

  • args (Array)

    months

Raises:

  • (::ArgumentError)


64
65
66
67
68
# File 'lib/recurring_date/enumerator.rb', line 64

def month(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:month)
end

#mweek(*args) ⇒ Object

Filter dates by a mweek of a date.

Parameters:

  • args (Array)

    mweeks

Raises:

  • (::ArgumentError)


56
57
58
59
60
# File 'lib/recurring_date/enumerator.rb', line 56

def mweek(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:mweek)
end

#not_matching(*args) ⇒ Object

Filter dates that do not match args with yielded block.

Parameters:

  • args (Array)

    arguments that are compared to the expession in given block.



134
135
136
# File 'lib/recurring_date/enumerator.rb', line 134

def not_matching(*args)
  reject { |date| args.include?(yield(date)) }
end

#not_mday(*args) ⇒ Object

Filter dates not maching mday.

Parameters:

  • args (Array)

    mdays

Raises:

  • (::ArgumentError)


88
89
90
91
92
# File 'lib/recurring_date/enumerator.rb', line 88

def not_mday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:mday)
end

#not_month(*args) ⇒ Object

Filter dates not maching month.

Parameters:

  • args (Array)

    months

Raises:

  • (::ArgumentError)


112
113
114
115
116
# File 'lib/recurring_date/enumerator.rb', line 112

def not_month(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:month)
end

#not_mweek(*args) ⇒ Object

Filter dates not maching mweek.

Parameters:

  • args (Array)

    mweeks

Raises:

  • (::ArgumentError)


104
105
106
107
108
# File 'lib/recurring_date/enumerator.rb', line 104

def not_mweek(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:mweek)
end

#not_wday(*args) ⇒ Object

Filter dates not maching wday.

Parameters:

  • args (Array)

    wdays

Raises:

  • (::ArgumentError)


96
97
98
99
100
# File 'lib/recurring_date/enumerator.rb', line 96

def not_wday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:wday)
end

#not_yday(*args) ⇒ Object

Filter dates not maching yday.

Parameters:

  • args (Array)

    ydays

Raises:

  • (::ArgumentError)


80
81
82
83
84
# File 'lib/recurring_date/enumerator.rb', line 80

def not_yday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:yday)
end

#not_year(*args) ⇒ Object

Filter dates not maching year.

Parameters:

  • args (Array)

    years

Raises:

  • (::ArgumentError)


120
121
122
123
124
# File 'lib/recurring_date/enumerator.rb', line 120

def not_year(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  not_matching(*args, &:year)
end

#pattern(*args) ⇒ Object

Filter dates by the sequence of occurences.

Parameters:

  • args (Array)

    occurence schema



140
141
142
143
144
# File 'lib/recurring_date/enumerator.rb', line 140

def pattern(*args)
  select_with_index do |_, i|
    args.any? { |arg| ((i + 1) % arg).zero? }
  end
end

#rejectObject

Filter dates by condition in given block. Opposite to select.



169
170
171
# File 'lib/recurring_date/enumerator.rb', line 169

def reject
  RecurringDate::Enumerator.new(self) { |result, value| result << value unless yield(value) }
end

#selectObject

Filter dates by condition in given block.



164
165
166
# File 'lib/recurring_date/enumerator.rb', line 164

def select
  RecurringDate::Enumerator.new(self) { |result, value| result << value if yield(value) }
end

#select_with_indexObject

Filter dates by condition in given block. Yields a date and its index.



155
156
157
158
159
160
161
# File 'lib/recurring_date/enumerator.rb', line 155

def select_with_index
  index = 0
  RecurringDate::Enumerator.new(self) do |result, value|
    result << value if yield(value, index)
    index += 1
  end
end

#take(number) ⇒ Object

Take only number of dates.

Parameters:

  • number (Integer)

Raises:

  • (::ArgumentError)


175
176
177
178
179
180
181
182
183
184
185
# File 'lib/recurring_date/enumerator.rb', line 175

def take(number)
  raise ::ArgumentError, 'not an integer' unless number.is_a?(::Integer)

  taken = number
  RecurringDate::Enumerator.new(self) do |result, value|
    raise ::StopIteration if taken.zero?

    result << value
    taken -= 1
  end
end

#take_whileObject

Take only dates until the expression in given block has been met.



188
189
190
191
192
193
194
# File 'lib/recurring_date/enumerator.rb', line 188

def take_while
  RecurringDate::Enumerator.new(self) do |result, value|
    raise ::StopIteration unless yield(value)

    result << value
  end
end

#until(date) ⇒ Object

Set upper bound of the date range (make the enumerator finite).

Parameters:

Raises:

  • (::ArgumentError)


148
149
150
151
152
# File 'lib/recurring_date/enumerator.rb', line 148

def until(date)
  raise ::ArgumentError, 'not a date' unless date.is_a?(::Date)

  take_while { |obj| obj <= date }
end

#wday(*args) ⇒ Object

Filter dates by a wday of a date.

Parameters:

  • args (Array)

    wdays

Raises:

  • (::ArgumentError)


48
49
50
51
52
# File 'lib/recurring_date/enumerator.rb', line 48

def wday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:wday)
end

#yday(*args) ⇒ Object

Filter dates by a yday of a date.

Parameters:

  • args (Array)

    ydays

Raises:

  • (::ArgumentError)


32
33
34
35
36
# File 'lib/recurring_date/enumerator.rb', line 32

def yday(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:yday)
end

#year(*args) ⇒ Object

Filter dates by a year of a date.

Parameters:

  • args (Array)

    years

Raises:

  • (::ArgumentError)


72
73
74
75
76
# File 'lib/recurring_date/enumerator.rb', line 72

def year(*args)
  raise ::ArgumentError, 'not an integer' unless args.all? { |arg| arg.is_a?(::Integer) }

  matching(*args, &:year)
end