recurring_date 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4cc8eff5210dd36470912daacacd4a4a44f7109
4
+ data.tar.gz: d339946cc56e30491139b5577a63033e963be901
5
+ SHA512:
6
+ metadata.gz: 4465192a86991d9f4cd3fb2d0d9eede1845ddc690cc39a2c9990d7dd0947c6935fdc2ea4d417e3856a399e15fa327514168f3813a5303aa92a99e11bb168f029
7
+ data.tar.gz: 31e51294b9b93401ae693aee6d2c61b01ebb25947835f5bd41d6e86bff79cf608dd69a9eba680de394e955459c4431152e788a5c8427a0fdb4d253381184f4e2
@@ -0,0 +1,15 @@
1
+ require_relative 'recurring_date_enumerator'
2
+
3
+ module RecurringDate
4
+ def mweek
5
+ (mday - 1) / 7 + 1
6
+ end
7
+
8
+ def to_enum
9
+ RecurringDateEnumerator.eternity(from: self)
10
+ end
11
+ end
12
+
13
+ class Date
14
+ include RecurringDate
15
+ end
@@ -0,0 +1,128 @@
1
+ require 'date'
2
+
3
+ class RecurringDateEnumerator < Enumerator
4
+ def initialize(range)
5
+ super() do |result|
6
+ begin
7
+ range.each { |val| block_given? ? yield(result, val) : result << val }
8
+ rescue StopIteration
9
+ nil
10
+ end
11
+ end
12
+ end
13
+
14
+ def self.eternity(from: Date.new(1970))
15
+ RecurringDateEnumerator.new(infinity(from))
16
+ end
17
+
18
+ def yday(*args)
19
+ matching(*args, &:yday)
20
+ end
21
+
22
+ def mday(*args)
23
+ matching(*args, &:mday)
24
+ end
25
+
26
+ def wday(*args)
27
+ matching(*args, &:wday)
28
+ end
29
+
30
+ def mweek(*args)
31
+ matching(*args, &:mweek)
32
+ end
33
+
34
+ def month(*args)
35
+ matching(*args, &:month)
36
+ end
37
+
38
+ def year(*args)
39
+ matching(*args, &:year)
40
+ end
41
+
42
+ def not_yday(*args)
43
+ not_matching(*args, &:yday)
44
+ end
45
+
46
+ def not_mday(*args)
47
+ not_matching(*args, &:mday)
48
+ end
49
+
50
+ def not_wday(*args)
51
+ not_matching(*args, &:wday)
52
+ end
53
+
54
+ def not_mweek(*args)
55
+ not_matching(*args, &:mweek)
56
+ end
57
+
58
+ def not_month(*args)
59
+ not_matching(*args, &:month)
60
+ end
61
+
62
+ def not_year(*args)
63
+ not_matching(*args, &:year)
64
+ end
65
+
66
+ def matching(*args)
67
+ select { |date| args.include?(yield(date)) }
68
+ end
69
+
70
+ def not_matching(*args)
71
+ reject { |date| args.include?(yield(date)) }
72
+ end
73
+
74
+ def pattern(*args)
75
+ select_with_index do |_, i|
76
+ args.any? do |arg|
77
+ ((i + 1) % arg).zero?
78
+ end
79
+ end
80
+ end
81
+
82
+ def until(arg)
83
+ take_while { |date| date <= arg.to_date }
84
+ end
85
+
86
+ def select_with_index
87
+ index = 0
88
+ RecurringDateEnumerator.new(self) do |result, value|
89
+ result << value if yield(value, index)
90
+ index += 1
91
+ end
92
+ end
93
+
94
+ def select
95
+ RecurringDateEnumerator.new(self) { |result, value| result << value if yield(value) }
96
+ end
97
+
98
+ def reject
99
+ RecurringDateEnumerator.new(self) { |result, value| result << value unless yield(value) }
100
+ end
101
+
102
+ def take(n)
103
+ taken = n
104
+ RecurringDateEnumerator.new(self) do |result, value|
105
+ raise StopIteration if taken.zero?
106
+ result << value
107
+ taken -= 1
108
+ end
109
+ end
110
+
111
+ def take_while
112
+ RecurringDateEnumerator.new(self) do |result, value|
113
+ raise StopIteration unless yield(value)
114
+ result << value
115
+ end
116
+ end
117
+
118
+ def inspect
119
+ ['#<RecurringDateEnumerator: 0x', object_id, '>'].join
120
+ end
121
+
122
+ def self.infinity(date)
123
+ Enumerator.new do |y|
124
+ i = date.prev_day
125
+ loop { y << (i = i.next) }
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recurring_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Placzyński
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Enumerator for recurring dates with specific patterns
14
+ email:
15
+ - should@not-fail.pl
16
+ - p.placzynski@binarapps.com
17
+ - placzynski.pawel@gmail.com
18
+ - silquenarmo@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/recurring_date.rb
24
+ - lib/recurring_date_enumerator.rb
25
+ homepage: ''
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.6.11
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Recurring dates enumerator
49
+ test_files: []