Skip to content

Commit 8b9b63d

Browse files
committed
Abstract out parsing YAML.
We always use the same code for loading YAML, but it was repeated in three places. This commit moves YAML parsing to the Util class.
1 parent f575204 commit 8b9b63d

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

lib/cff/file.rb

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

3-
# Copyright (c) 2018-2022 The Ruby Citation File Format Developers.
3+
# Copyright (c) 2018-2024 The Ruby Citation File Format Developers.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
require_relative 'version'
2020

2121
require 'date'
22-
require 'yaml'
2322

2423
##
2524
module CFF
@@ -71,9 +70,7 @@ def self.read(file)
7170
content = ::File.read(file)
7271
comment = File.parse_comment(content)
7372

74-
new(
75-
file, YAML.safe_load(content, permitted_classes: [Date, Time]), comment
76-
)
73+
new(file, Util.parse_yaml(content), comment)
7774
end
7875

7976
# :call-seq:
@@ -91,7 +88,7 @@ def self.open(file)
9188
if ::File.exist?(file)
9289
content = ::File.read(file)
9390
comment = File.parse_comment(content)
94-
yaml = YAML.safe_load(content, permitted_classes: [Date, Time])
91+
yaml = Util.parse_yaml(content)
9592
else
9693
comment = CFF_COMMENT
9794
yaml = ''

lib/cff/index.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright (c) 2018-2022 The Ruby Citation File Format Developers.
3+
# Copyright (c) 2018-2024 The Ruby Citation File Format Developers.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -25,8 +25,6 @@
2525
require_relative 'validatable'
2626
require_relative 'citable'
2727

28-
require 'yaml'
29-
3028
##
3129
module CFF
3230
# Index is the core data structure for a CITATION.cff file. It can be
@@ -100,7 +98,7 @@ def initialize(param)
10098
#
10199
# Read a CFF Index from a String and parse it for subsequent manipulation.
102100
def self.read(index)
103-
new(YAML.safe_load(index, permitted_classes: [Date, Time]))
101+
new(Util.parse_yaml(index))
104102
end
105103

106104
# :call-seq:

lib/cff/util.rb

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

3-
# Copyright (c) 2018-2022 The Ruby Citation File Format Developers.
3+
# Copyright (c) 2018-2024 The Ruby Citation File Format Developers.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
require_relative 'version'
2020

2121
require 'rubygems'
22+
require 'yaml'
2223

2324
##
2425
module CFF
@@ -30,6 +31,10 @@ module Util
3031

3132
module_function
3233

34+
def parse_yaml(string)
35+
YAML.safe_load(string, permitted_classes: [Date, Time])
36+
end
37+
3338
def update_cff_version(version)
3439
return '' if version.nil? || version.empty?
3540

0 commit comments

Comments
 (0)