lib/nanoc3/base/source_data/site.rb in nanoc3-3.2.0a3 vs lib/nanoc3/base/source_data/site.rb in nanoc3-3.2.0a4
- old
+ new
@@ -49,10 +49,12 @@
end
# Compiles the site.
#
# @return [void]
+ #
+ # @since 3.2.0
def compile
compiler.run
end
# Returns the compiler for this site. Will create a new compiler if none
@@ -165,40 +167,21 @@
# useful for online data sources; for example, a Twitter data source
# would need the username of the account from which to fetch tweets.
#
# @return [Hash] The site configuration
def config
- # Add reference to config if necessary
- if !@config.respond_to?(:reference)
- def @config.reference
- :config
- end
- end
-
- # Add data to config if necessary
- if !@config.respond_to?(:data)
- def @config.data
- self.inspect
- end
- end
-
@config
end
# Fills each item's parent reference and children array with the
# appropriate items. It is probably not necessary to call this method
# manually; it will be called when appropriate.
#
# @return [void]
def setup_child_parent_links
- # Clear all links
+ teardown_child_parent_links
@items.each do |item|
- item.parent = nil
- item.children = []
- end
-
- @items.each do |item|
# Get parent
parent_identifier = item.identifier.sub(/[^\/]+\/$/, '')
parent = @items.find { |p| p.identifier == parent_identifier }
next if parent.nil? or item.identifier == '/'
@@ -206,17 +189,27 @@
item.parent = parent
parent.children << item
end
end
+ # Removes all child-parent links.
+ #
+ # @api private
+ #
+ # @return [void]
+ def teardown_child_parent_links
+ @items.each do |item|
+ item.parent = nil
+ item.children = []
+ end
+ end
+
# Prevents all further modifications to itself, its items, its layouts etc.
#
# @return [void]
def freeze
- super
-
- config.freeze
+ config.freeze_recursively
items.each { |i| i.freeze }
layouts.each { |l| l.freeze }
code_snippets.each { |cs| cs.freeze }
end
@@ -231,12 +224,12 @@
#
# @api private
#
# @return [void]
def load
- return if @data_loaded
- @data_loaded = true
+ return if @loaded || @loading
+ @loading = true
# Load all data
load_code_snippets
data_sources.each { |ds| ds.use }
load_items
@@ -245,12 +238,39 @@
setup_child_parent_links
# Load compiler too
# FIXME this should not be necessary
compiler.load
+
+ @loaded = true
+ rescue => e
+ unload
+ raise e
+ ensure
+ @loading = false
end
+ # Undoes the effects of {#load}. Used when {#load} raises an exception.
+ #
+ # @api private
+ def unload
+ return if @unloading
+ @unloading = true
+
+ @items_loaded = false
+ @items = []
+ @layouts_loaded = false
+ @layouts = []
+ @code_snippets_loaded = false
+ @code_snippets = []
+
+ compiler.unload
+
+ @loaded = false
+ @unloading = false
+ end
+
private
# Loads this site’s code and executes it.
def load_code_snippets
@code_snippets_loaded ||= false
@@ -317,10 +337,13 @@
# Use passed config hash
@config = DEFAULT_CONFIG.merge(dir_or_config_hash)
end
# Merge data sources with default data source config
- @config[:data_sources].map! { |ds| DEFAULT_DATA_SOURCE_CONFIG.merge(ds) }
+ @config[:data_sources] = @config[:data_sources].map { |ds| DEFAULT_DATA_SOURCE_CONFIG.merge(ds) }
+
+ # Convert to proper configuration
+ @config = Nanoc3::Configuration.new(@config)
end
end
end