Class: RecurringDateEnumerator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ RecurringDateEnumerator

Returns a new instance of RecurringDateEnumerator.



4
5
6
7
8
9
10
11
12
# File 'lib/recurring_date_enumerator.rb', line 4

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

Class Method Details

.eternity(from: Date.new(1970)) ⇒ Object



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

def self.eternity(from: Date.new(1970))
  RecurringDateEnumerator.new(infinity(from))
end

.infinity(date) ⇒ Object



122
123
124
125
126
127
# File 'lib/recurring_date_enumerator.rb', line 122

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

Instance Method Details

#inspectObject



118
119
120
# File 'lib/recurring_date_enumerator.rb', line 118

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

#matching(*args) ⇒ Object



66
67
68
# File 'lib/recurring_date_enumerator.rb', line 66

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

#mday(*args) ⇒ Object



22
23
24
# File 'lib/recurring_date_enumerator.rb', line 22

def mday(*args)
  matching(*args, &:mday)
end

#month(*args) ⇒ Object



34
35
36
# File 'lib/recurring_date_enumerator.rb', line 34

def month(*args)
  matching(*args, &:month)
end

#mweek(*args) ⇒ Object



30
31
32
# File 'lib/recurring_date_enumerator.rb', line 30

def mweek(*args)
  matching(*args, &:mweek)
end

#not_matching(*args) ⇒ Object



70
71
72
# File 'lib/recurring_date_enumerator.rb', line 70

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

#not_mday(*args) ⇒ Object



46
47
48
# File 'lib/recurring_date_enumerator.rb', line 46

def not_mday(*args)
  not_matching(*args, &:mday)
end

#not_month(*args) ⇒ Object



58
59
60
# File 'lib/recurring_date_enumerator.rb', line 58

def not_month(*args)
  not_matching(*args, &:month)
end

#not_mweek(*args) ⇒ Object



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

def not_mweek(*args)
  not_matching(*args, &:mweek)
end

#not_wday(*args) ⇒ Object



50
51
52
# File 'lib/recurring_date_enumerator.rb', line 50

def not_wday(*args)
  not_matching(*args, &:wday)
end

#not_yday(*args) ⇒ Object



42
43
44
# File 'lib/recurring_date_enumerator.rb', line 42

def not_yday(*args)
  not_matching(*args, &:yday)
end

#not_year(*args) ⇒ Object



62
63
64
# File 'lib/recurring_date_enumerator.rb', line 62

def not_year(*args)
  not_matching(*args, &:year)
end

#pattern(*args) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/recurring_date_enumerator.rb', line 74

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

#rejectObject



98
99
100
# File 'lib/recurring_date_enumerator.rb', line 98

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

#selectObject



94
95
96
# File 'lib/recurring_date_enumerator.rb', line 94

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

#select_with_indexObject



86
87
88
89
90
91
92
# File 'lib/recurring_date_enumerator.rb', line 86

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

#take(n) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/recurring_date_enumerator.rb', line 102

def take(n)
  taken = n
  RecurringDateEnumerator.new(self) do |result, value|
    raise StopIteration if taken.zero?
    result << value
    taken -= 1
  end
end

#take_whileObject



111
112
113
114
115
116
# File 'lib/recurring_date_enumerator.rb', line 111

def take_while
  RecurringDateEnumerator.new(self) do |result, value|
    raise StopIteration unless yield(value)
    result << value
  end
end

#until(arg) ⇒ Object



82
83
84
# File 'lib/recurring_date_enumerator.rb', line 82

def until(arg)
  take_while { |date| date <= arg.to_date }
end

#wday(*args) ⇒ Object



26
27
28
# File 'lib/recurring_date_enumerator.rb', line 26

def wday(*args)
  matching(*args, &:wday)
end

#yday(*args) ⇒ Object



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

def yday(*args)
  matching(*args, &:yday)
end

#year(*args) ⇒ Object



38
39
40
# File 'lib/recurring_date_enumerator.rb', line 38

def year(*args)
  matching(*args, &:year)
end