class Utopia::Content::Links::Resolver

Represents a list of {Link} instances relating to the structure of the content. They are formed from the `links.yaml` file and the actual directory structure on disk.

Constants

DEFAULT_INDEX_INFO

Attributes

named[R]
ordered[R]
top[R]

Public Class Methods

new(links, top = Path.root) click to toggle source
# File lib/utopia/content/links.rb, line 154
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

Public Instance Methods

each(locale) { |find{|link| locale == locale}| ... } click to toggle source
# File lib/utopia/content/links.rb, line 187
def each(locale)
        return to_enum(:each, locale) unless block_given?
        
        ordered.each do |links|
                yield links.find{|link| link.locale == locale}
        end
end
indices() click to toggle source
# File lib/utopia/content/links.rb, line 183
def indices
        return @ordered.select{|link| link.index?}
end
lookup(name, locale = nil) click to toggle source
# File lib/utopia/content/links.rb, line 195
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

Private Instance Methods

entries(path) click to toggle source
# File lib/utopia/content/links.rb, line 214
def entries(path)
        Dir.entries(path).reject{|entry| entry.match(/^[\._]/)}
end
load_default_index(name = INDEX, info = {}) { |link(:index, name, nil, to_directory, info, path)| ... } click to toggle source

The default index for a directory which has no contents.

# File lib/utopia/content/links.rb, line 251
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
load_directory(name, metadata) { |link(:directory, name, locale, path, merge)| ... } click to toggle source

@param name [String] the name of the directory.

# File lib/utopia/content/links.rb, line 219
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
load_file(name, locale, info) { |link(:file, name, locale, path, info)| ... } click to toggle source
# File lib/utopia/content/links.rb, line 264
def load_file(name, locale, info)
        info ||= {}
        
        if locale and defaults = @metadata[name]
                info = defaults.merge(info)
        end
        
        path = @top + name
        
        yield Link.new(:file, name, locale, path, info)
end
load_index(name, locale, info) { |link(:index, name, locale, path, info, path)| ... } click to toggle source
# File lib/utopia/content/links.rb, line 236
def load_index(name, locale, info)
        info ||= {}
        
        if locale and defaults = @metadata[name]
                info = defaults.merge(info)
        end
        
        path = @top + name
        
        yield Link.new(:index, name, locale, path, info, path[-2])
end
load_virtuals(metadata) { |link(:virtual, name, locale, path, info)| ... } click to toggle source
# File lib/utopia/content/links.rb, line 276
def load_virtuals(metadata)
        virtuals = {}
        
        # After processing all directory entries, we are left with virtual links:
        metadata.each do |key, info|
                name, locale = key.split('.', 2)
                localized = (virtuals[name] ||= {})
                localized[locale] = info
        end
        
        virtuals.each do |name, localized|
                defaults = localized[nil]
                
                localized.each do |locale, info|
                        info = defaults&.merge(info) || info
                        path = info[:path]
                        
                        yield Link.new(:virtual, name, locale, path, info)
                end
        end
end