lib/yaml_extend.rb in yaml_extend-0.1.1 vs lib/yaml_extend.rb in yaml_extend-0.2.0

- old
+ new

@@ -8,21 +8,46 @@ # # Extending the YAML library to allow to inherit from another YAML file(s) # module YAML + # default path in the yaml file where the files to inherit from are defined + DEFAULT_INHERITANCE_KEY = 'extends' + @@ext_load_key = nil # + # Set a custom inheritance key globally once. + # So you don't need to specify it on every call of ext_load_file() + # + # @param key [String|Array<String>|nil] the key in the yaml file, containing the file strings to extend from. Set nil or call #reset_load_key to reset the key. + def self.ext_load_key=(key) + if key.is_a?(String) || key.is_a?(Array) || key.nil? + @@ext_load_key = key + else + raise "Parameter 'key' must be of type String or Array<String> or nil" + end + end + + # + # Reset the ext_load_key and use the default settings + # + def self.reset_load_key() + @@ext_load_key = nil + end + # # Extended variant of the #load_file method by providing the # ability to inherit from other YAML file(s) # # @param yaml_path [String] the path to the yaml file to be loaded # @param inheritance_key [String|Array] The key used in the yaml file to extend from another YAML file. Use an Array if you want to use a tree structure key like "options.extends" => ['options','extends'] # @param extend_existing_arrays [Boolean] extend existing arrays instead of replacing them # @param config [Hash] a hash to be merged into the result, usually only recursivly called by the method itself # # @return [Hash] the resulting yaml config # - def self.ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true, config = {}) + def self.ext_load_file(yaml_path, inheritance_key=nil, extend_existing_arrays=true, config = {}) + if inheritance_key.nil? + inheritance_key = @@ext_load_key || DEFAULT_INHERITANCE_KEY + end total_config ||= {} yaml_path = YAML.make_absolute_path yaml_path super_config = YAML.load_file(File.open(yaml_path)) super_inheritance_files = yaml_value_by_key inheritance_key, super_config delete_yaml_key inheritance_key, super_config # we don't merge the super inheritance keys into the base yaml