class ExtendedYAML
Constants
- VERSION
Attributes
Public Class Methods
@param [String] file path to YAML file @param [String] key YAML key to use for loading files @return [Hash, Array] the parsed YAML
# File lib/extended_yaml.rb, line 12 def self.load(file, key: 'extends') new(file, key: key).result end
# File lib/extended_yaml.rb, line 16 def initialize(file, key: 'extends') @file, @key = file, key end
Public Instance Methods
@return [String] the YAML string, with evaluated and ERB
# File lib/extended_yaml.rb, line 27 def evaluate ERB.new(File.read file).result end
@return [Hash, Array, nil] the parsed YAML
# File lib/extended_yaml.rb, line 21 def result data = ::YAML.load evaluate data ? resolve_extends(data) : nil end
Private Instance Methods
# File lib/extended_yaml.rb, line 37 def base_dir @base_dir ||= File.dirname file end
Receives a string or an array of strings, each representing an acceptable path definition. Each definition may be with or without a file etxension, and may be a glob pattern. The resulting array will be a normalized list of full paths.
@param [Array, String] files one or more path definitions @return [Array] a normalized list of absolute paths
# File lib/extended_yaml.rb, line 63 def expand_file_list(files) list = [] files = [files] unless files.is_a? Array files.each do |path| list += expand_path path end list end
@param [String] path the path to the YAML file, with or without extension.
May include a glob pattern wildcard.
@return [Array] one or more absolute paths.
# File lib/extended_yaml.rb, line 77 def expand_path(path) path += extension unless path.end_with? extension path = File.expand_path path, base_dir path.include?('*') ? Dir[path] : [path] end
# File lib/extended_yaml.rb, line 33 def extension @extension ||= File.extname file end
@param [Hash] data the data structure, possibly with 'extends' array @return [Hash] the merged data
# File lib/extended_yaml.rb, line 43 def resolve_extends(data) extra_files = data.delete key return data unless extra_files extra_files = expand_file_list extra_files extra_files.each do |path| data = self.class.new(path, key: key).result.deep_merge data end data end