Class: RecurringDate::Enumerator
- Inherits:
-
Enumerator
- Object
- Enumerator
- RecurringDate::Enumerator
- Defined in:
- lib/recurring_date/enumerator.rb
Overview
The enumerator over the dates.
Class Method Summary collapse
-
.eternity ⇒ Object
Returns a lazy, perpetual enumerator over dates, day by day, from 1st January 1970.
-
.from(date) ⇒ Object
Returns a lazy, perpetual enumerator over dates, day by day, from
date
. -
.infinity(date) ⇒ Object
The actual enumerator.
Instance Method Summary collapse
-
#initialize(range) ⇒ Enumerator
constructor
Initiate the enumerator.
-
#inspect ⇒ Object
Dummy inspect for enumerator.
-
#matching(*args) ⇒ Object
Filter dates that matches
args
with yielded block. -
#mday(*args) ⇒ Object
Filter dates by a
mday
of a date. -
#month(*args) ⇒ Object
Filter dates by a
month
of a date. -
#mweek(*args) ⇒ Object
Filter dates by a
mweek
of a date. -
#not_matching(*args) ⇒ Object
Filter dates that do not match
args
with yielded block. -
#not_mday(*args) ⇒ Object
Filter dates not maching
mday
. -
#not_month(*args) ⇒ Object
Filter dates not maching
month
. -
#not_mweek(*args) ⇒ Object
Filter dates not maching
mweek
. -
#not_wday(*args) ⇒ Object
Filter dates not maching
wday
. -
#not_yday(*args) ⇒ Object
Filter dates not maching
yday
. -
#not_year(*args) ⇒ Object
Filter dates not maching
year
. -
#pattern(*args) ⇒ Object
Filter dates by the sequence of occurences.
-
#reject ⇒ Object
Filter dates by condition in given block.
-
#select ⇒ Object
Filter dates by condition in given block.
-
#select_with_index ⇒ Object
Filter dates by condition in given block.
-
#take(number) ⇒ Object
Take only
number
of dates. -
#take_while ⇒ Object
Take only dates until the expression in given block has been met.
-
#until(date) ⇒ Object
Set upper bound of the date range (make the enumerator finite).
-
#wday(*args) ⇒ Object
Filter dates by a
wday
of a date. -
#yday(*args) ⇒ Object
Filter dates by a
yday
of a date. -
#year(*args) ⇒ Object
Filter dates by a
year
of a date.
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
.eternity ⇒ Object
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
.
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
.
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
#inspect ⇒ Object
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.
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.
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.
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.
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.
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
.
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
.
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
.
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
.
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
.
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
.
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.
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 |
#reject ⇒ Object
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 |
#select ⇒ Object
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_index ⇒ Object
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.
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_while ⇒ Object
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).
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.
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.
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.
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 |