kalendor 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/kalendor.gemspec +1 -1
- data/lib/kalendor/factory.rb +13 -7
- data/lib/kalendor/instance/annual.rb +2 -2
- data/lib/kalendor/instance/composite.rb +1 -2
- data/lib/kalendor/instance/date_list.rb +2 -2
- data/lib/kalendor/instance/interval.rb +2 -2
- data/lib/kalendor/instance/month.rb +2 -2
- data/lib/kalendor/instance/monthly.rb +10 -0
- data/lib/kalendor/instance/subtract.rb +2 -2
- data/lib/kalendor/instance/union.rb +3 -0
- data/lib/kalendor/instance/weekday.rb +2 -2
- data/lib/kalendor/instance.rb +20 -0
- data/lib/kalendor/monthly.rb +37 -0
- data/lib/kalendor/version.rb +1 -1
- data/spec/append_spec.rb +32 -0
- data/spec/intersect_spec.rb +12 -0
- data/spec/monthly_spec.rb +29 -0
- metadata +11 -7
- data/lib/kalendor/named.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8444d0694397d073104ded49efd8c0f3d67cd4a2cff9227214b35c9cc2a01b34
|
4
|
+
data.tar.gz: 488d82824e4643d8802f4a6707bd074ff74973e8c12ac14fc6c890ebcfc67565
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9599ebca8914d341d2c94acc78461131d5aadbfa869599bad0902e045427ee2b673c6bf5bdc880ad2a5bd1badf4415af2f18f62234ad26d191cae9898364ac2
|
7
|
+
data.tar.gz: 180e1b7ed41294247dffd69ef5ed0e52ca50ef80a343398c105fde2ebe13044a58b29bc24944622f5484fb772a12876c10f850ba4e3632e455e59d0b3729a357
|
data/kalendor.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency 'aduki'
|
20
|
+
spec.add_dependency 'aduki', ">= 0.2.7"
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency 'rspec', '>= 2.9'
|
data/lib/kalendor/factory.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require "aduki"
|
2
2
|
require "kalendor/date_helper"
|
3
|
-
require "kalendor/named"
|
4
3
|
require "kalendor/instance/date_list"
|
5
4
|
require "kalendor/instance/weekday"
|
6
5
|
require "kalendor/instance/annual"
|
7
6
|
require "kalendor/instance/month"
|
7
|
+
require "kalendor/instance/monthly"
|
8
8
|
require "kalendor/instance/union"
|
9
9
|
require "kalendor/instance/intersect"
|
10
10
|
require "kalendor/instance/subtract"
|
@@ -14,20 +14,18 @@ module Kalendor
|
|
14
14
|
class Factory
|
15
15
|
include DateHelper
|
16
16
|
|
17
|
-
def named name, label, kal
|
18
|
-
Kalendor::Named.new name: name, label: label, kalendor: kal
|
19
|
-
end
|
20
|
-
|
21
17
|
def annual date, month
|
22
18
|
Kalendor::Instance::Annual.new annual_month: month, annual_date: date
|
23
19
|
end
|
24
20
|
|
25
21
|
def union *schedules
|
26
|
-
|
22
|
+
s = schedules.compact
|
23
|
+
s.length == 1 ? s[0] : Kalendor::Instance::Union.new(schedules: s)
|
27
24
|
end
|
28
25
|
|
29
26
|
def intersect *schedules
|
30
|
-
|
27
|
+
s = schedules.compact
|
28
|
+
s.length == 1 ? s[0] : Kalendor::Instance::Intersect.new(schedules: s)
|
31
29
|
end
|
32
30
|
|
33
31
|
def subtract x, y
|
@@ -50,6 +48,14 @@ module Kalendor
|
|
50
48
|
Kalendor::Instance::Month.new month: n
|
51
49
|
end
|
52
50
|
|
51
|
+
def monthly d
|
52
|
+
Kalendor::Instance::Monthly.new monthly_date: d
|
53
|
+
end
|
54
|
+
|
55
|
+
def append existing, other
|
56
|
+
existing.append other
|
57
|
+
end
|
58
|
+
|
53
59
|
def subtract? x, y ; y ? subtract(x, y) : x ; end
|
54
60
|
alias date to_date
|
55
61
|
end
|
@@ -2,8 +2,8 @@ require 'kalendor/date_list'
|
|
2
2
|
|
3
3
|
module Kalendor
|
4
4
|
module Instance
|
5
|
-
class DateList
|
6
|
-
include
|
5
|
+
class DateList < Kalendor::Instance::Base
|
6
|
+
include Kalendor::DateList
|
7
7
|
attr_accessor :dates
|
8
8
|
def aduki_after_initialize ; self.dates = array_wrap(dates || []).map { |d| to_date d }.compact ; end
|
9
9
|
end
|
@@ -2,8 +2,8 @@ require 'kalendor'
|
|
2
2
|
require 'kalendor/weekday'
|
3
3
|
|
4
4
|
module Kalendor::Instance
|
5
|
-
class Weekday
|
6
|
-
include
|
5
|
+
class Weekday < Kalendor::Instance::Base
|
6
|
+
include Kalendor::Weekday
|
7
7
|
attr_accessor :weekday, :nth_of_month
|
8
8
|
end
|
9
9
|
end
|
data/lib/kalendor/instance.rb
CHANGED
@@ -1,4 +1,24 @@
|
|
1
1
|
module Kalendor
|
2
2
|
module Instance
|
3
|
+
class Base < Aduki::Initializable
|
4
|
+
attr_accessor :name, :label
|
5
|
+
|
6
|
+
def append other
|
7
|
+
me = self
|
8
|
+
Kalendor.build { union me, other }
|
9
|
+
end
|
10
|
+
|
11
|
+
def union *others
|
12
|
+
Kalendor::Instance::Union.new(schedules: [self, *others])
|
13
|
+
end
|
14
|
+
|
15
|
+
def intersect *others
|
16
|
+
Kalendor::Instance::Intersect.new(schedules: [self, *others])
|
17
|
+
end
|
18
|
+
|
19
|
+
def subtract other
|
20
|
+
Kalendor::Instance::Subtract.new include_dates: self, exclude_dates: other
|
21
|
+
end
|
22
|
+
end
|
3
23
|
end
|
4
24
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Kalendor
|
2
|
+
module Monthly
|
3
|
+
include Instance
|
4
|
+
def advance_to_next_month date, monthly_date
|
5
|
+
m = date.month
|
6
|
+
|
7
|
+
# j f m a m j j a s o n d
|
8
|
+
# 31 28 31 30 31 30 31 31 30 31 30 31
|
9
|
+
# safe months are months (except december) followed by a 31-day month, we just increment the month
|
10
|
+
return Date.new(date.year, m + 1, date.day) if [2,4,6,7,9,11].include?(m)
|
11
|
+
|
12
|
+
# december is safe, we increment the year and set month to january
|
13
|
+
return Date.new(date.year + 1, 1, date.day) if m == 12
|
14
|
+
|
15
|
+
|
16
|
+
# m must be in [1,3,5,8,10], ie must be a 31-day month preceding a non-31-day month
|
17
|
+
# not sure if following month contains the required date
|
18
|
+
|
19
|
+
begin
|
20
|
+
Date.new(date.year, date.month + 1, monthly_date) # try advance one month
|
21
|
+
rescue
|
22
|
+
Date.new(date.year, date.month + 2, monthly_date) # otherwise advance two months
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_dates from, upto
|
27
|
+
first = Date.new(from.year, from.month, monthly_date)
|
28
|
+
first = advance_to_next_month(first, monthly_date) if first < from
|
29
|
+
result = []
|
30
|
+
while first && (first <= upto)
|
31
|
+
result << first
|
32
|
+
first = advance_to_next_month(first, monthly_date)
|
33
|
+
end
|
34
|
+
result
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/kalendor/version.rb
CHANGED
data/spec/append_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'kalendor'
|
2
|
+
require 'kalendor/instance/annual'
|
3
|
+
require 'kalendor/instance/union'
|
4
|
+
require 'kalendor/instance/weekday'
|
5
|
+
|
6
|
+
RSpec.describe "#append" do
|
7
|
+
it "produces every weekend in oct - dec 2016" do
|
8
|
+
sats = Kalendor.build { weekday(6) }
|
9
|
+
suns = Kalendor.build { weekday(7) }
|
10
|
+
|
11
|
+
weekends = Kalendor.build { append sats, suns }
|
12
|
+
|
13
|
+
weekends_in_oct_2016 = [1,2,8,9,15,16,22,23,29,30].map { |d| date("2016-10-#{d}") }
|
14
|
+
weekends_in_nov_2016 = [5,6,12,13,19,20,26,27 ].map { |d| date("2016-11-#{d}") }
|
15
|
+
weekends_in_dec_2016 = [3,4,10,11,17,18,24,25,31 ].map { |d| date("2016-12-#{d}") }
|
16
|
+
weekends_in_end_2016 = weekends_in_oct_2016 + weekends_in_nov_2016 + weekends_in_dec_2016
|
17
|
+
|
18
|
+
expect(weekends.get_dates date("2016-10-01"), date("2016-12-31")).to eq weekends_in_end_2016
|
19
|
+
end
|
20
|
+
|
21
|
+
it "appends to a union" do
|
22
|
+
mix = Kalendor.build { union annual(12,3), annual(21,6), annual(8,6) }
|
23
|
+
|
24
|
+
more = Kalendor.build { append mix, annual(18,11) }
|
25
|
+
|
26
|
+
bd2015 = %w{ 2015-03-12 2015-06-08 2015-06-21 2015-11-18 }
|
27
|
+
bd2016 = %w{ 2016-03-12 2016-06-08 2016-06-21 2016-11-18 }
|
28
|
+
expected = (bd2015 + bd2016).map { |d| date d }.sort
|
29
|
+
|
30
|
+
expect(more.get_dates date("2015-01-01"), date("2016-12-31")).to eq expected
|
31
|
+
end
|
32
|
+
end
|
data/spec/intersect_spec.rb
CHANGED
@@ -17,4 +17,16 @@ RSpec.describe Kalendor::Intersect do
|
|
17
17
|
|
18
18
|
expect(summer_weekends.get_dates(date("1999-01-01"), date("2020-12-31")).to_a).to eq weekends_in_summer_2016
|
19
19
|
end
|
20
|
+
|
21
|
+
it "ignores nil arguments" do
|
22
|
+
summer_weekends = Kalendor.build do
|
23
|
+
intersect(nil, union(weekday(6), weekday(7)), nil, nil)
|
24
|
+
end
|
25
|
+
|
26
|
+
weekends_in_jul_2016 = [30,31 ].map { |d| date("2016-07-#{d}") }
|
27
|
+
weekends_in_aug_2016 = [6,7,13,14,20,21].map { |d| date("2016-08-#{d}") }
|
28
|
+
weekends_in_summer_2016 = weekends_in_jul_2016 + weekends_in_aug_2016
|
29
|
+
|
30
|
+
expect(summer_weekends.get_dates(date("2016-07-29"), date("2016-08-26")).to_a).to eq weekends_in_summer_2016
|
31
|
+
end
|
20
32
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'kalendor'
|
2
|
+
require 'kalendor/monthly'
|
3
|
+
require 'kalendor/instance/monthly'
|
4
|
+
|
5
|
+
RSpec.describe Kalendor::Monthly do
|
6
|
+
it "generates birthdays over a number of years" do
|
7
|
+
a = Kalendor.build { monthly 18 }
|
8
|
+
birthdays = a.get_dates(date("1960-01-01"), date("1960-05-31"))
|
9
|
+
expect(birthdays).to eq [date("1960-01-18"), date("1960-02-18"), date("1960-03-18"), date("1960-04-18"), date("1960-05-18"), ]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "skips months that don't have the date" do
|
13
|
+
a = Kalendor.build { monthly 31 }
|
14
|
+
birthdays = a.get_dates(date("2020-01-01"), date("2020-08-31"))
|
15
|
+
expect(birthdays).to eq [date("2020-01-31"), date("2020-03-31"), date("2020-05-31"), date("2020-07-31"), date("2020-08-31"), ]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "jumps to next month if to late for first month" do
|
19
|
+
a = Kalendor.build { monthly 18 }
|
20
|
+
birthdays = a.get_dates(date("1960-01-20"), date("1960-03-31"))
|
21
|
+
expect(birthdays).to eq [date("1960-02-18"), date("1960-03-18"), ]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns nothing if period too small and before date" do
|
25
|
+
a = Kalendor.build { monthly 18 }
|
26
|
+
birthdays = a.get_dates(date("1960-10-20"), date("1960-11-10"))
|
27
|
+
expect(birthdays).to eq []
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalendor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aduki
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.2.7
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.2.7
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/kalendor/instance/intersect.rb
|
108
108
|
- lib/kalendor/instance/interval.rb
|
109
109
|
- lib/kalendor/instance/month.rb
|
110
|
+
- lib/kalendor/instance/monthly.rb
|
110
111
|
- lib/kalendor/instance/store.rb
|
111
112
|
- lib/kalendor/instance/subtract.rb
|
112
113
|
- lib/kalendor/instance/union.rb
|
@@ -114,17 +115,19 @@ files:
|
|
114
115
|
- lib/kalendor/intersect.rb
|
115
116
|
- lib/kalendor/interval.rb
|
116
117
|
- lib/kalendor/month.rb
|
117
|
-
- lib/kalendor/
|
118
|
+
- lib/kalendor/monthly.rb
|
118
119
|
- lib/kalendor/subtract.rb
|
119
120
|
- lib/kalendor/union.rb
|
120
121
|
- lib/kalendor/version.rb
|
121
122
|
- lib/kalendor/weekday.rb
|
122
123
|
- spec/annual_spec.rb
|
124
|
+
- spec/append_spec.rb
|
123
125
|
- spec/date_list_spec.rb
|
124
126
|
- spec/date_spec.rb
|
125
127
|
- spec/intersect_spec.rb
|
126
128
|
- spec/interval_spec.rb
|
127
129
|
- spec/month_spec.rb
|
130
|
+
- spec/monthly_spec.rb
|
128
131
|
- spec/spec_helper.rb
|
129
132
|
- spec/subtract_spec.rb
|
130
133
|
- spec/union_spec.rb
|
@@ -149,18 +152,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
152
|
- !ruby/object:Gem::Version
|
150
153
|
version: '0'
|
151
154
|
requirements: []
|
152
|
-
|
153
|
-
rubygems_version: 2.5.2.3
|
155
|
+
rubygems_version: 3.0.3.1
|
154
156
|
signing_key:
|
155
157
|
specification_version: 4
|
156
158
|
summary: Utility classes for generating sets of dates
|
157
159
|
test_files:
|
158
160
|
- spec/annual_spec.rb
|
161
|
+
- spec/append_spec.rb
|
159
162
|
- spec/date_list_spec.rb
|
160
163
|
- spec/date_spec.rb
|
161
164
|
- spec/intersect_spec.rb
|
162
165
|
- spec/interval_spec.rb
|
163
166
|
- spec/month_spec.rb
|
167
|
+
- spec/monthly_spec.rb
|
164
168
|
- spec/spec_helper.rb
|
165
169
|
- spec/subtract_spec.rb
|
166
170
|
- spec/union_spec.rb
|