kalendor 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97a6837962ca266b30b478cf4914dfa97d3767b0
4
- data.tar.gz: e48e46ba66ae1b2197bbabdf492d6ac2bb0c4551
3
+ metadata.gz: 8233c57889dc42f00dbd60c1710b90cfebbf9ac7
4
+ data.tar.gz: d1c5696401d654c0391a3e846439540255f4cf4d
5
5
  SHA512:
6
- metadata.gz: fb03842893c6239cb14fcbcdad293a5aa8017effa0e457b2c2880bc5f14cb27d0654f555f593f5cd5b2517716ea1234739d5a29f53c7cb961d4d393ee4d60b15
7
- data.tar.gz: e38a91e96fb31279264a1991e915c405d29e51a2a7dc8c4583b3a922e683d4d1941604eb48323fa9bbd6b8bd1bc87f56bf18189056ebb0a0457656aaced5bfb0
6
+ metadata.gz: c313972414214c589920aeb3948fa7452ce178cd67df4aa5e122c777226f81a4cb2bf6d83370b530e153e3a41c8f1e73950b056ea9ea38caceef34e7f3dec03b
7
+ data.tar.gz: fb37990fead79b1c33c5322b67281c913aad8ad0b8afa3e69031b4f2f96929492f37c86cf022bd91d619db9de8d35792dc1ae8016d28015a9bd64ced9db38d8c
@@ -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'
@@ -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
- Kalendor::Instance::Union.new schedules: schedules
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
- Kalendor::Instance::Intersect.new schedules: schedules
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,10 @@ 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
+
53
55
  def subtract? x, y ; y ? subtract(x, y) : x ; end
54
56
  alias date to_date
55
57
  end
@@ -1,4 +1,7 @@
1
1
  module Kalendor
2
2
  module Instance
3
+ class Base < Aduki::Initializable
4
+ attr_accessor :name, :label
5
+ end
3
6
  end
4
7
  end
@@ -2,8 +2,8 @@ require 'kalendor/annual'
2
2
 
3
3
  module Kalendor
4
4
  module Instance
5
- class Annual
6
- include Aduki::Initializer, Kalendor::Annual
5
+ class Annual < Kalendor::Instance::Base
6
+ include Kalendor::Annual
7
7
  attr_accessor :annual_date, :annual_month
8
8
  end
9
9
  end
@@ -1,7 +1,6 @@
1
1
  require 'aduki'
2
2
  require 'kalendor'
3
3
 
4
- class Kalendor::Instance::Composite
5
- include Aduki::Initializer
4
+ class Kalendor::Instance::Composite < Kalendor::Instance::Base
6
5
  attr_accessor :schedules
7
6
  end
@@ -2,8 +2,8 @@ require 'kalendor/date_list'
2
2
 
3
3
  module Kalendor
4
4
  module Instance
5
- class DateList
6
- include Aduki::Initializer, Kalendor::DateList
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/interval'
2
2
 
3
3
  module Kalendor
4
4
  module Instance
5
- class Interval
6
- include Aduki::Initializer, Kalendor::Interval
5
+ class Interval < Kalendor::Instance::Base
6
+ include Kalendor::Interval
7
7
  attr_accessor :interval_from, :interval_upto
8
8
  end
9
9
  end
@@ -2,8 +2,8 @@ require 'kalendor/month'
2
2
 
3
3
  module Kalendor
4
4
  module Instance
5
- class Month
6
- include Aduki::Initializer, Kalendor::Month
5
+ class Month < Kalendor::Instance::Base
6
+ include Kalendor::Month
7
7
  attr_accessor :month
8
8
  end
9
9
  end
@@ -0,0 +1,10 @@
1
+ require 'kalendor/monthly'
2
+
3
+ module Kalendor
4
+ module Instance
5
+ class Monthly < Kalendor::Instance::Base
6
+ include Kalendor::Monthly
7
+ attr_accessor :monthly_date
8
+ end
9
+ end
10
+ end
@@ -2,8 +2,8 @@ require 'kalendor/subtract'
2
2
 
3
3
  module Kalendor
4
4
  module Instance
5
- class Subtract
6
- include Aduki::Initializer, Kalendor::Subtract
5
+ class Subtract < Kalendor::Instance::Base
6
+ include Kalendor::Subtract
7
7
  attr_accessor :include_dates, :exclude_dates
8
8
  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 Aduki::Initializer, Kalendor::Weekday
5
+ class Weekday < Kalendor::Instance::Base
6
+ include Kalendor::Weekday
7
7
  attr_accessor :weekday, :nth_of_month
8
8
  end
9
9
  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
@@ -1,3 +1,3 @@
1
1
  module Kalendor
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-21 00:00:00.000000000 Z
11
+ date: 2020-01-31 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: '0'
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: '0'
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,7 +115,7 @@ files:
114
115
  - lib/kalendor/intersect.rb
115
116
  - lib/kalendor/interval.rb
116
117
  - lib/kalendor/month.rb
117
- - lib/kalendor/named.rb
118
+ - lib/kalendor/monthly.rb
118
119
  - lib/kalendor/subtract.rb
119
120
  - lib/kalendor/union.rb
120
121
  - lib/kalendor/version.rb
@@ -125,6 +126,7 @@ files:
125
126
  - spec/intersect_spec.rb
126
127
  - spec/interval_spec.rb
127
128
  - spec/month_spec.rb
129
+ - spec/monthly_spec.rb
128
130
  - spec/spec_helper.rb
129
131
  - spec/subtract_spec.rb
130
132
  - spec/union_spec.rb
@@ -161,6 +163,7 @@ test_files:
161
163
  - spec/intersect_spec.rb
162
164
  - spec/interval_spec.rb
163
165
  - spec/month_spec.rb
166
+ - spec/monthly_spec.rb
164
167
  - spec/spec_helper.rb
165
168
  - spec/subtract_spec.rb
166
169
  - spec/union_spec.rb
@@ -1,8 +0,0 @@
1
- module Kalendor
2
- class Named
3
- include Aduki::Initializer
4
- attr_accessor :name, :label, :kalendor
5
- def get_dates from, upto ; kalendor.get_dates from, upto ; end
6
- def _nydp_get m ; return name if m == :name ; end
7
- end
8
- end