Skip to content

Commit 32ff145

Browse files
scpikepacso
andauthored
Add backwards compatibility to Ruby 2.6 (#562)
* Add backwards compatibility to Ruby 2.6 This removes use of the "anonymous block argument" feature which is only supported in Ruby 3.1+ so that the library stays backwards compatible to earlier ruby versions. * Update test matrix * Update CHANGELOG.md * Backwards compatibility changes * Backwards compatibility changes - named splat parameter --------- Co-authored-by: Jon Pascoe <jon.pascoe@me.com>
1 parent ce99543 commit 32ff145

8 files changed

Lines changed: 87 additions & 16 deletions

File tree

.github/workflows/tests.yaml

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,57 @@ jobs:
4242
- lint
4343
strategy:
4444
matrix:
45-
rails: ['7.2', '8.0', '8.1']
46-
ruby: ['3.2', '3.3', '3.4', '4.0']
45+
rails: ['6.1', '7.0', '7.1', '7.2', '8.0', '8.1']
46+
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4', '4.0']
47+
exclude:
48+
# Rails 6.1: Only supports Ruby 2.6-3.0
49+
- rails: '6.1'
50+
ruby: '3.1'
51+
- rails: '6.1'
52+
ruby: '3.2'
53+
- rails: '6.1'
54+
ruby: '3.3'
55+
- rails: '6.1'
56+
ruby: '3.4'
57+
- rails: '6.1'
58+
ruby: '4.0'
59+
# Rails 7.0 & 7.1: Require Ruby 2.7+, tested up to 3.3
60+
- rails: '7.0'
61+
ruby: '2.6'
62+
- rails: '7.0'
63+
ruby: '3.4'
64+
- rails: '7.0'
65+
ruby: '4.0'
66+
- rails: '7.1'
67+
ruby: '2.6'
68+
- rails: '7.1'
69+
ruby: '3.4'
70+
- rails: '7.1'
71+
ruby: '4.0'
72+
# Rails 7.2: Requires Ruby 3.1+
73+
- rails: '7.2'
74+
ruby: '2.6'
75+
- rails: '7.2'
76+
ruby: '2.7'
77+
- rails: '7.2'
78+
ruby: '3.0'
79+
# Rails 8.0 & 8.1: Require Ruby 3.2+
80+
- rails: '8.0'
81+
ruby: '2.6'
82+
- rails: '8.0'
83+
ruby: '2.7'
84+
- rails: '8.0'
85+
ruby: '3.0'
86+
- rails: '8.0'
87+
ruby: '3.1'
88+
- rails: '8.1'
89+
ruby: '2.6'
90+
- rails: '8.1'
91+
ruby: '2.7'
92+
- rails: '8.1'
93+
ruby: '3.0'
94+
- rails: '8.1'
95+
ruby: '3.1'
4796
runs-on: ubuntu-latest
4897
env:
4998
RAILS_VERSION: ${{ matrix.rails }}

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Support for BYSETPOS with all rule frequencies (yearly, monthly, weekly, daily, hourly, minutely, secondly). ([#449](https://github.com/ice-cube-ruby/ice_cube/pull/449)) by [@nehresma](https://github.com/nehresma) and [@NicolasMarlier](https://github.com/NicolasMarlier)
1010

1111
### Changed
12-
- Updated CI test matrix to support Rails 7.2, 8.0, 8.1 and Ruby 3.2, 3.3, 3.4, 4.0 by [@nehresma](https://github.com/nehresma) and [@pacso](https://github.com/pacso)
12+
- Updated CI test matrix to support Rails 7.2, 8.0, 8.1 and Ruby 3.2, 3.3, 3.4, 4.0. ([#565](https://github.com/ice-cube-ruby/ice_cube/pull/565)) by [@nehresma](https://github.com/nehresma) and [@pacso](https://github.com/pacso)
13+
- Added backwards compatibility for Ruby 2.6+ by replacing anonymous block arguments with explicit block parameters and adding Rails/Ruby version compatibility exclusions in CI test matrix. ([#562](https://github.com/ice-cube-ruby/ice_cube/pull/562)) by [@scpike](https://github.com/scpike)
1314

1415
## [0.17.0] - 2024-07-18
1516
### Added

lib/ice_cube/input_alignment.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ def initialize(rule, value, rule_part)
88

99
attr_reader :rule, :value, :rule_part
1010

11-
def verify(freq, options = {}, &)
11+
def verify(freq, options = {}, &block)
1212
@rule.validations[:interval] or return
1313

1414
case @rule
1515
when DailyRule
16-
verify_wday_alignment(freq, &)
16+
verify_wday_alignment(freq, &block)
1717
when MonthlyRule
18-
verify_month_alignment(freq, &)
18+
verify_month_alignment(freq, &block)
1919
else
20-
verify_freq_alignment(freq, &)
20+
verify_freq_alignment(freq, &block)
2121
end
2222
end
2323

lib/ice_cube/parsers/yaml_parser.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ class YamlParser < HashParser
77
attr_reader :hash
88

99
def initialize(yaml)
10-
@hash = YAML.safe_load(yaml, permitted_classes: [Date, Symbol, Time], aliases: true)
10+
# Ruby 2.6-3.0 use positional args, Ruby 3.1+ uses keyword args for YAML.safe_load
11+
@hash = if RUBY_VERSION < "3.1"
12+
YAML.safe_load(yaml, [Date, Symbol, Time], [], true)
13+
else
14+
YAML.safe_load(yaml, permitted_classes: [Date, Symbol, Time], aliases: true)
15+
end
1116
yaml.match SERIALIZED_START do |match|
1217
start_time = hash[:start_time] || hash[:start_date]
1318
TimeUtil.restore_deserialized_offset start_time, match[:tz]

lib/ice_cube/rule.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ def self.from_ical(ical)
3636
end
3737

3838
# Yaml implementation
39-
def to_yaml(*)
40-
YAML.dump(to_hash, *)
39+
def to_yaml(*args)
40+
YAML.dump(to_hash, *args)
4141
end
4242

4343
# From yaml
4444
def self.from_yaml(yaml)
45-
from_hash YAML.safe_load(yaml, permitted_classes: [Date, Symbol, Time])
45+
# Ruby 2.6-3.0 use positional args, Ruby 3.1+ uses keyword args for YAML.safe_load
46+
if RUBY_VERSION < "3.1"
47+
from_hash YAML.safe_load(yaml, [Date, Symbol, Time])
48+
else
49+
from_hash YAML.safe_load(yaml, permitted_classes: [Date, Symbol, Time])
50+
end
4651
end
4752

4853
def to_hash

lib/ice_cube/schedule.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ def all_occurrences_enumerator
160160
end
161161

162162
# Iterate forever
163-
def each_occurrence(&)
164-
enumerate_occurrences(start_time, &).to_a
163+
def each_occurrence(&block)
164+
enumerate_occurrences(start_time, &block).to_a
165165
self
166166
end
167167

spec/examples/serialization_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
let(:start_time) { Time.now.in_time_zone("America/Vancouver") }
1616

1717
it "serializes time as a Hash" do
18-
hash = YAML.safe_load(yaml, permitted_classes: [Symbol, Time])
18+
# Ruby 2.6-3.0 use positional args, Ruby 3.1+ uses keyword args for YAML.safe_load
19+
hash = if RUBY_VERSION < "3.1"
20+
YAML.safe_load(yaml, [Symbol, Time])
21+
else
22+
YAML.safe_load(yaml, permitted_classes: [Symbol, Time])
23+
end
1924
expect(hash[:start_time][:time]).to eq start_time.utc
2025
expect(hash[:start_time][:zone]).to eq "America/Vancouver"
2126
end

spec/examples/to_yaml_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,14 @@ module IceCube
375375

376376
symbol_yaml = Schedule.from_hash(symbol_data).to_yaml
377377
string_yaml = Schedule.from_hash(string_data).to_yaml
378-
expect(YAML.safe_load(symbol_yaml, permitted_classes: [Symbol, Time]))
379-
.to eq(YAML.safe_load(string_yaml, permitted_classes: [Symbol, Time]))
378+
# Ruby 2.6-3.0 use positional args, Ruby 3.1+ uses keyword args for YAML.safe_load
379+
if RUBY_VERSION < "3.1"
380+
expect(YAML.safe_load(symbol_yaml, [Symbol, Time]))
381+
.to eq(YAML.safe_load(string_yaml, [Symbol, Time]))
382+
else
383+
expect(YAML.safe_load(symbol_yaml, permitted_classes: [Symbol, Time]))
384+
.to eq(YAML.safe_load(string_yaml, permitted_classes: [Symbol, Time]))
385+
end
380386
end
381387

382388
it "should raise an ArgumentError when trying to deserialize an invalid rule type" do

0 commit comments

Comments
 (0)