lib/yaml_extend.rb in yaml_extend-1.2.0 vs lib/yaml_extend.rb in yaml_extend-1.2.1

- old
+ new

@@ -13,19 +13,19 @@ # default path in the yaml file where the files to inherit from are defined DEFAULT_INHERITANCE_KEY = 'extends' @@ext_load_key = nil DEEP_MERGE_OPTIONS = [ - :preserve_unmergeables, - :knockout_prefix, - :overwrite_arrays, - :sort_merged_arrays, - :unpack_arrays, - :merge_hash_arrays, - :extend_existing_arrays, - :merge_nil_values, - :merge_debug, + :preserve_unmergeables, + :knockout_prefix, + :overwrite_arrays, + :sort_merged_arrays, + :unpack_arrays, + :merge_hash_arrays, + :extend_existing_arrays, + :merge_nil_values, + :merge_debug, ] # # Set a custom inheritance key globally once. # So you don't need to specify it on every call of ext_load_file() @@ -84,23 +84,23 @@ def self.ext_load_file_recursive(yaml_path, inheritance_key, options = {}, config) # Allow also class Pathname instead of class String yaml_path = yaml_path.to_s # backward compatibility to 1.0.1 if options == true || options == false - options = {extend_existing_arrays: options} + options = { extend_existing_arrays: options } end default_options = { - preserve_inheritance_key: false, - preserve_unmergeables: false, - knockout_prefix: nil, - overwrite_arrays: false, - sort_merged_arrays: false, - unpack_arrays: nil, - merge_hash_arrays: false, - extend_existing_arrays: true, - merge_nil_values: false, - merge_debug: false, + preserve_inheritance_key: false, + preserve_unmergeables: false, + knockout_prefix: nil, + overwrite_arrays: false, + sort_merged_arrays: false, + unpack_arrays: nil, + merge_hash_arrays: false, + extend_existing_arrays: true, + merge_nil_values: false, + merge_debug: false, } options = default_options.merge options private_class_method if inheritance_key.nil? inheritance_key = @@ext_load_key || DEFAULT_INHERITANCE_KEY @@ -109,13 +109,21 @@ yaml_path = YAML.make_absolute_path yaml_path super_config = if yaml_path.match(/(\.erb\.|\.erb$)/) - YAML.unsafe_load(ERB.new(File.read(yaml_path)).result) + if YAML.respond_to? :unsafe_load # backward compatibility for Ruby 3.1 / Psych 4 + YAML.unsafe_load(ERB.new(File.read(yaml_path)).result) + else + YAML.load(ERB.new(File.read(yaml_path)).result) + end else - YAML.unsafe_load_file(File.open(yaml_path)) + if YAML.respond_to? :unsafe_load_file # backward compatibility for Ruby 3.1 / Psych 4 + YAML.unsafe_load_file(File.open(yaml_path)) + else + YAML.load_file(File.open(yaml_path)) + end end super_inheritance_files = yaml_value_by_key inheritance_key, super_config unless options[:preserve_inheritance_key] delete_yaml_key inheritance_key, super_config # we don't merge the super inheritance keys into the base yaml @@ -147,11 +155,12 @@ # [0] is the call from ext_load_file_recursive, # [1] is inside ext_load_file, # [2] is the external caller of YAML.ext_load_file base_path = if defined?(caller_locations) File.dirname(caller_locations[2].path) - else # Fallback for ruby < 2.1.10 + else + # Fallback for ruby < 2.1.10 File.dirname(caller[2]) end return base_path + '/' + file_path if File.exist? base_path + '/' + file_path # relative path from yaml file return Dir.pwd + '/' + file_path if File.exist? Dir.pwd + '/' + file_path # relative path from project error_message = "Can not find absolute path of '#{file_path}'" @@ -163,11 +172,11 @@ end def self.absolute_path?(path) private_class_method path.start_with?('/') || # unix like - (path.length >= 3 && path[1] == ':') # ms windows + (path.length >= 3 && path[1] == ':') # ms windows end # Return the value of the corresponding key # @param key [String|Array] def self.yaml_value_by_key(key, config) @@ -185,10 +194,10 @@ end end def self.valid_key_type?(key) key.is_a?(Array) || key.is_a?(String) || - raise(InvalidKeyTypeError, "Invalid key of type '#{key.class.name}'. Valid types are String and Array.") + raise(InvalidKeyTypeError, "Invalid key of type '#{key.class.name}'. Valid types are String and Array.") end def self.delete_yaml_key(key, config) return config.delete(key) if key.is_a? String cfg_ref = config