From 5720e04f2ba5111f1b7d8a5909dc898bbf72b7d3 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Mon, 26 Jul 2021 16:04:49 +0100 Subject: [PATCH] Ruby 2.5 compatibility --- lib/front_matter_parser/loader/yaml.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/front_matter_parser/loader/yaml.rb b/lib/front_matter_parser/loader/yaml.rb index c547a6a..48285fe 100644 --- a/lib/front_matter_parser/loader/yaml.rb +++ b/lib/front_matter_parser/loader/yaml.rb @@ -19,7 +19,20 @@ def initialize(allowlist_classes: []) # @param string [String] front matter string representation # @return [Hash] front matter hash representation def call(string) - YAML.safe_load(string, permitted_classes: allowlist_classes) + if safe_load_with_permitted_classes_arg? + YAML.safe_load(string, permitted_classes: allowlist_classes) + else + YAML.safe_load(string, allowlist_classes) + end + end + + def safe_load_with_permitted_classes_arg? + # This `permitted_classes` keyword argument was + # introduced in Ruby 2.6, and therefore is not + # compatible with Ruby 2.5 and earlier. + YAML + .public_method(:safe_load).parameters + .any? { |type, name| type == :key && name == :permitted_classes } end end end