Skip to content

Commit 9e29a21

Browse files
committed
Backwards compatibility changes
1 parent fe2c336 commit 9e29a21

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ def to_yaml(*)
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

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)