lib/nanoc/base/source_data/site.rb in nanoc-3.7.3 vs lib/nanoc/base/source_data/site.rb in nanoc-3.7.4

- old
+ new

@@ -33,14 +33,14 @@ # `DEFAULT_CONFIG`. DEFAULT_CONFIG = { :text_extensions => %w( css erb haml htm html js less markdown md php rb sass scss txt xhtml xml coffee hb handlebars mustache ms slim ).sort, :lib_dirs => %w( lib ), :output_dir => 'output', - :data_sources => [ {} ], - :index_filenames => [ 'index.html' ], + :data_sources => [{}], + :index_filenames => ['index.html'], :enable_output_diff => false, - :prune => { :auto_prune => false, :exclude => [ '.git', '.hg', '.svn', 'CVS' ] } + :prune => { :auto_prune => false, :exclude => ['.git', '.hg', '.svn', 'CVS'] } } # Creates a site object for the site specified by the given # `dir_or_config_hash` argument. # @@ -187,18 +187,18 @@ item_map[item.identifier] = item end @items.each do |item| parent_id_end = item.identifier.rindex('/', -2) - if parent_id_end - parent_id = item.identifier[0..parent_id_end] - parent = item_map[parent_id] - if parent - item.parent = parent - parent.children << item - end - end + next unless parent_id_end + + parent_id = item.identifier[0..parent_id_end] + parent = item_map[parent_id] + next unless parent + + item.parent = parent + parent.children << item end end # Removes all child-parent links. # @@ -222,11 +222,11 @@ code_snippets.each { |cs| cs.freeze } end # @deprecated It is no longer necessary to explicitly load site data. It # is safe to remove all {#load_data} calls. - def load_data(force = false) + def load_data(_force = false) warn 'It is no longer necessary to call Nanoc::Site#load_data. This method no longer has any effect. All calls to this method can be safely removed.' end # Loads the site data. It is not necessary to call this method explicitly; # it will be called when it is necessary. @@ -238,14 +238,14 @@ return if @loaded || @loading @loading = true # Load all data load_code_snippets - data_sources.each { |ds| ds.use } - load_items - load_layouts - data_sources.each { |ds| ds.unuse } + with_datasources do + load_items + load_layouts + end setup_child_parent_links # Ensure unique ensure_identifier_uniqueness(@items, 'item') ensure_identifier_uniqueness(@layouts, 'layout') @@ -295,12 +295,21 @@ def self.config_filename_for_cwd filenames = %w( nanoc.yaml config.yaml ) filenames.find { |f| File.file?(f) } end - private + private + # Executes the given block, making sure that the datasources are + # available for the duration of the block + def with_datasources(&block) + data_sources.each { |ds| ds.use } + yield + ensure + data_sources.each { |ds| ds.unuse } + end + # Loads this site’s code and executes it. def load_code_snippets @code_snippets_loaded ||= false return if @code_snippets_loaded @code_snippets_loaded = true @@ -363,10 +372,10 @@ def apply_parent_config(config, config_paths = []) parent_config_file = config[:parent_config_file] if parent_config_file config.delete(:parent_config_file) config_path = File.absolute_path(parent_config_file, File.dirname(config_paths.last)) - if !File.file?(config_path) + unless File.file?(config_path) raise Nanoc::Errors::GenericTrivial, "Could not find parent configuration file '#{parent_config_file}'" end if config_paths.include?(config_path) raise Nanoc::Errors::GenericTrivial, "Cycle detected. Could not use parent configuration file '#{parent_config_file}'" end