lib/frise/loader.rb in frise-0.4.0 vs lib/frise/loader.rb in frise-0.4.1

- old
+ new

@@ -11,25 +11,28 @@ # The load method loads a configuration file, merges the applicable includes and validates its schema. class Loader def initialize(include_sym: '$include', content_include_sym: '$content_include', schema_sym: '$schema', + delete_sym: '$delete', pre_loaders: [], validators: nil, exit_on_fail: true) @include_sym = include_sym @content_include_sym = content_include_sym @schema_sym = schema_sym + @delete_sym = delete_sym @pre_loaders = pre_loaders @validators = validators @exit_on_fail = exit_on_fail @defaults_loader = DefaultsLoader.new( include_sym: include_sym, content_include_sym: content_include_sym, - schema_sym: schema_sym + schema_sym: schema_sym, + delete_sym: delete_sym ) end def load(config_file, global_vars = {}) config = Parser.parse(config_file, global_vars) @@ -37,12 +40,12 @@ @pre_loaders.each do |pre_loader| config = pre_loader.call(config) end - config = process_includes(config, [], config, global_vars) if @include_sym - config = process_schemas(config, [], global_vars) if @schema_sym + config = process_includes(config, [], config, global_vars) unless @include_sym.nil? + config = process_schemas(config, [], global_vars) unless @schema_sym.nil? config end private @@ -63,22 +66,23 @@ end # process $include directives config, next_include_confs = extract_include(config, at_path) include_confs = next_include_confs + include_confs_stack - if include_confs.empty? - config.map { |k, v| [k, process_includes(v, at_path + [k], root_config, global_vars)] }.to_h - else - Lazy.new do - include_conf = include_confs.first - rest_include_confs = include_confs[1..-1] - symbol_table = build_symbol_table(root_config, at_path, config, global_vars, include_conf) - included_config = Parser.parse(include_conf['file'], symbol_table) - config = @defaults_loader.merge_defaults_obj(config, included_config) - process_includes(config, at_path, merge_at(root_config, at_path, config), global_vars, rest_include_confs) - end - end + res = if include_confs.empty? + config.map { |k, v| [k, process_includes(v, at_path + [k], root_config, global_vars)] }.to_h + else + Lazy.new do + include_conf = include_confs.first + rest_include_confs = include_confs[1..-1] + symbol_table = build_symbol_table(root_config, at_path, config, global_vars, include_conf) + included_config = Parser.parse(include_conf['file'], symbol_table) + config = @defaults_loader.merge_defaults_obj(config, included_config) + process_includes(config, at_path, merge_at(root_config, at_path, config), global_vars, rest_include_confs) + end + end + @delete_sym.nil? ? res : omit_deleted(res) end def process_schema_includes(schema, at_path, global_vars) return schema unless schema.class == Hash @@ -158,20 +162,31 @@ return config.merge(to_merge) if at_path.empty? head, *tail = at_path config.merge(head => merge_at(config[head], tail, to_merge)) end + # returns the config without the keys whose values are @delete_sym + def omit_deleted(config) + config.each_with_object({}) do |(k, v), new_hash| + if v.is_a?(Hash) + new_hash[k] = omit_deleted(v) + else + new_hash[k] = v unless v == @delete_sym + end + end + end + # builds the symbol table for the Liquid renderization of a file, based on: # - `root_config`: the root of the whole config # - `at_path`: the current path # - `config`: the config subtree being built # - `global_vars`: the global variables # - `include_conf`: the $include or $content_include configuration def build_symbol_table(root_config, at_path, config, global_vars, include_conf) extra_vars = (include_conf['vars'] || {}).map { |k, v| [k, root_config.dig(*v.split('.'))] }.to_h extra_consts = include_conf['constants'] || {} - (config ? merge_at(root_config, at_path, config) : root_config) + omit_deleted(config ? merge_at(root_config, at_path, config) : root_config) .merge(global_vars) .merge(extra_vars) .merge(extra_consts) .merge('_this' => config) end