Skip to content

Commit 29558de

Browse files
authored
Remove active support dependency (#41)
1 parent 3908fc9 commit 29558de

28 files changed

Lines changed: 339 additions & 159 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.bundle
33
html
44
*.lock
5+
coverage

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ inherit_gem:
33
rubocop-fnando: .rubocop.yml
44

55
AllCops:
6-
TargetRubyVersion: 2.5
6+
TargetRubyVersion: 3.3
77
NewCops: enable
88
Exclude:
99
- bin/**/*

lib/recurrence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require "recurrence/namespace"
3+
require_relative "recurrence/namespace"
44

55
# The default namespace. If you already have Recurrence constant set on your
66
# codebase, you can inherit from `Recurrence_` and have your own namespace.

lib/recurrence/event.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/recurrence/event/base.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ def next!
4141
@date = next_in_recurrence
4242

4343
@finished = true if @options[:through] && @date >= @options[:through]
44+
4445
if @date > @options[:until]
4546
@finished = true
4647
@date = nil
4748
end
49+
4850
shift_to @date if @date && @options[:shift]
51+
4952
@date
5053
end
5154

@@ -79,25 +82,26 @@ def finished?
7982

8083
# Common validation for inherited classes.
8184
#
82-
private def valid_month_day?(day)
83-
raise ArgumentError, "invalid day #{day}" unless (1..31).cover?(day)
85+
private def validate_month_day(day)
86+
return if (1..31).cover?(day)
87+
88+
raise ArgumentError, "invalid day: #{day.inspect}"
8489
end
8590

8691
# Check if the given key has a valid weekday (0 upto 6) or a valid weekday
8792
# name (defined in the DAYS constant). If a weekday name (String) is
8893
# given, convert it to a weekday (Integer).
8994
#
90-
private def valid_weekday_or_weekday_name?(value)
95+
private def expand_weekday!(value)
9196
if value.is_a?(Numeric)
9297
unless (0..6).cover?(value)
93-
raise ArgumentError,
94-
"invalid day #{value}"
98+
raise ArgumentError, "invalid weekday: #{value}"
9599
end
96100

97101
value
98102
else
99103
weekday = WEEKDAYS[value.to_s]
100-
raise ArgumentError, "invalid weekday #{value}" unless weekday
104+
raise ArgumentError, "invalid weekday: #{value}" unless weekday
101105

102106
weekday
103107
end

lib/recurrence/event/monthly.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ class Monthly < Base # :nodoc: all
1212

1313
private def validate
1414
if @options.key?(:weekday)
15-
1615
# Allow :on => :last, :weekday => :thursday contruction.
1716
if @options[:on].to_s == "last"
1817
@options[:on] = 5
1918
elsif @options[:on].is_a?(Numeric)
20-
valid_week?(@options[:on])
19+
validate_week(@options[:on])
2120
else
22-
valid_ordinal?(@options[:on])
21+
validate_ordinal(@options[:on])
2322
@options[:on] = ORDINALS.index(@options[:on].to_s) + 1
2423
end
2524

26-
@options[:weekday] =
27-
valid_weekday_or_weekday_name?(@options[:weekday])
25+
@options[:weekday] = expand_weekday!(@options[:weekday])
2826
else
29-
valid_month_day?(@options[:on])
27+
validate_month_day(@options[:on])
3028
end
3129

3230
return unless @options[:interval].is_a?(Symbol)
3331

34-
valid_interval?(@options[:interval])
32+
validate_interval(@options[:interval])
3533
@options[:interval] = INTERVALS[@options[:interval]]
3634
end
3735

@@ -40,7 +38,7 @@ class Monthly < Base # :nodoc: all
4038

4139
type = @options.key?(:weekday) ? :weekday : :monthday
4240

43-
class_eval <<-METHOD, __FILE__, __LINE__ + 1
41+
singleton_class.class_eval <<-METHOD, __FILE__, __LINE__ + 1
4442
# private def next_month
4543
# if initialized?
4644
# advance_to_month_by_weekday(@date)
@@ -105,23 +103,25 @@ class Monthly < Base # :nodoc: all
105103
end
106104

107105
private def shift_to(date)
108-
@options[:on] = date.day
106+
@options[:on] = date.day unless @options[:weekday]
109107
end
110108

111-
private def valid_ordinal?(ordinal)
109+
private def validate_ordinal(ordinal)
112110
return if ORDINALS.include?(ordinal.to_s)
113111

114-
raise ArgumentError, "invalid ordinal #{ordinal}"
112+
raise ArgumentError, "invalid ordinal: #{ordinal}"
115113
end
116114

117-
private def valid_interval?(interval)
115+
private def validate_interval(interval)
118116
return if INTERVALS.key?(interval)
119117

120-
raise ArgumentError, "invalid ordinal #{interval}"
118+
raise ArgumentError, "invalid ordinal: #{interval.inspect}"
121119
end
122120

123-
private def valid_week?(week)
124-
raise ArgumentError, "invalid week #{week}" unless (1..5).cover?(week)
121+
private def validate_week(week)
122+
return if (1..5).cover?(week)
123+
124+
raise ArgumentError, "invalid week: #{week.inspect}"
125125
end
126126
end
127127
end

lib/recurrence/event/weekly.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class Recurrence_
44
module Event
55
class Weekly < Base # :nodoc: all
66
private def validate
7-
@options[:on] = Array.wrap(@options[:on]).inject([]) do |days, value|
8-
days << valid_weekday_or_weekday_name?(value)
7+
@options[:on] = Array(@options[:on]).inject([]) do |days, value|
8+
days << expand_weekday!(value)
99
end
1010

1111
@options[:on].sort!

lib/recurrence/event/yearly.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class Yearly < Base # :nodoc: all
1919
}.freeze
2020

2121
private def validate
22-
valid_month_day?(@options[:on].last)
22+
validate_month_day(@options[:on]&.last)
2323

2424
if @options[:on].first.is_a?(Numeric)
25-
valid_month?(@options[:on].first)
25+
validate_month(@options[:on].first)
2626
else
27-
valid_month_name?(@options[:on].first)
27+
validate_month_name(@options[:on].first)
2828
@options[:on] = [MONTHS[@options[:on].first.to_s], @options[:on].last]
2929
end
3030
end
@@ -50,16 +50,16 @@ class Yearly < Base # :nodoc: all
5050
@options[:on] = [date.month, date.day]
5151
end
5252

53-
private def valid_month?(month)
53+
private def validate_month(month)
5454
return if (1..12).cover?(month)
5555

56-
raise ArgumentError, "invalid month #{month}"
56+
raise ArgumentError, "invalid month: #{month.inspect}"
5757
end
5858

59-
private def valid_month_name?(month)
59+
private def validate_month_name(month)
6060
return if MONTHS.key?(month.to_s)
6161

62-
raise ArgumentError, "invalid month #{month}"
62+
raise ArgumentError, "invalid month: #{month.inspect}"
6363
end
6464
end
6565
end

lib/recurrence/handler.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/recurrence/handler/fall_back.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Handler # :nodoc: all
1515
# # => February 28, 2011
1616
#
1717
module FallBack
18+
using Refinements
19+
1820
def self.call(day, month, year)
1921
Date.new(year, month, [day, Time.days_in_month(month, year)].min)
2022
end

0 commit comments

Comments
 (0)