UtopiaSourceUtopiaContentLinksResolver

class Resolver

Represents a list of class Utopia::Content::Link instances relating to the structure of the content. They are formed from the links.yaml file and the actual directory structure on disk.

Definitions

def initialize(links, top = Path.root)

Resolve and order links from a content directory and its metadata.

Signature

parameter links Object

The links.

parameter top Object

The top.

Implementation

def initialize(links, top = Path.root)
	raise ArgumentError.new("top path must be absolute") unless top.absolute?
	
	@links = links
	
	@top = top
	
	# top.components.first == '', but this isn't a problem here.
	@path = File.join(links.root, top.components)
	
	@ordered = []
	@named = {}
	
	if File.directory?(@path)
		@metadata = links.metadata(@path)
		
		load_links(@metadata.dup) do |link|
			@ordered << link
			(@named[link.name] ||= []) << link
		end
	else
		@metadata = nil
	end
end

def indices

Select index-document links.

Signature

returns Array(Link)

The index-document links.

Implementation

def indices
	return @ordered.select{|link| link.index?}
end

def each(locale)

Enumerate the contained values.

Signature

parameter locale String

The locale.

returns Enumerator

An enumerator over the resulting values.

Implementation

def each(locale)
	return to_enum(:each, locale) unless block_given?
	
	ordered.each do |links|
		yield links.find{|link| link.locale == locale}
	end
end

def lookup(name, locale = nil)

Lookup.

Signature

parameter name String

The name.

parameter locale String

The locale.

returns Link | Nil

The exact locale match, or the unlocalized link as a fallback.

Implementation

def lookup(name, locale = nil)
	# This allows generic links to serve any locale requested.
	if links = @named[name]
		generic_link = nil
		
		links.each do |link|
			if link.locale == locale
				return link
			elsif link.locale.nil?
				generic_link = link
			end
		end
		
		return generic_link
	end
end

def load_directory(name, metadata, &block)

Implementation

def load_directory(name, metadata, &block)
	defaults = metadata.delete(name) || {}
	
	links = @links.links(@top + name).indices
	
	links.each do |link|
		# We extract the metadata according to the localized link:
		if info = metadata.delete("#{name}.#{link.locale}")
			info = info.merge(link.info)
		else
			info = link.info
		end
		
		yield Link.new(:directory, name, link.locale, link.path, defaults.merge(info))
	end
end

def load_default_index(name = INDEX, info = {})

The default index for a directory which has no contents.

Implementation

def load_default_index(name = INDEX, info = {})
	path = @top + name
	
	if info
		info = DEFAULT_INDEX_INFO.merge(info)
	else
		info = DEFAULT_INDEX_INFO
	end
	
	# Specify a nil uri if no index could be found for the directory:
	yield Link.new(:index, name, nil, @top.to_directory, info, path[-2])
end