class Utopia::Content::Links

Constants

Attributes

extension[R]
file_filter[R]
index_filter[R]
root[R]

Public Class Methods

for(root, path, locale = nil) click to toggle source
# File lib/utopia/content/links.rb, line 34
def self.for(root, path, locale = nil)
        warn "Using uncached links metadata!"
        self.new(root).for(path, locale)
end
index(root, path, **options) click to toggle source
# File lib/utopia/content/links.rb, line 39
def self.index(root, path, **options)
        warn "Using uncached links metadata!"
        self.new(root).index(path, **options)
end
new(root, extension: XNODE_EXTENSION) click to toggle source
# File lib/utopia/content/links.rb, line 44
def initialize(root, extension: XNODE_EXTENSION)
        @root = root
        
        @extension = extension
        @file_filter = /\A(?<key>(?<name>[^.]+)(\.(?<locale>.+))?)#{Regexp.escape extension}\Z/
        @index_filter = /\A(?<key>(?<name>index)(\.(?<locale>.+))?)#{Regexp.escape extension}\Z/
        
        @metadata_cache = Concurrent::Map.new
        @links_cache = Concurrent::Map.new
end

Public Instance Methods

for(path, locale = nil) click to toggle source

Resolve a link for the specified path, which must be a path to a specific link.

for(Path["/index"])
# File lib/utopia/content/links.rb, line 61
def for(path, locale = nil)
        links(path.dirname).lookup(path.last, locale)
end
index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false) click to toggle source

Give an index of all links that can be reached from the given path.

# File lib/utopia/content/links.rb, line 66
def index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false)
        ordered = links(path).ordered.dup
        
        # Ignore specific kinds of links:
        ignore = []
        ignore << :directory unless directories
        ignore << :file unless files
        ignore << :virtual unless virtuals
        ignore << :index unless indices
        
        if ignore.any?
                ordered.reject!{|link| ignore.include?(link.kind)}
        end
        
        # Filter links by display key:
        if display
                ordered.reject!{|link| link.info[display] == false}
        end
        
        # Filter links by name:
        if name
                # We use pattern === name, which matches either the whole string, or matches a regexp.
                ordered.select!{|link| name === link.name}
        end
        
        # Filter by locale:
        if locale
                locales = {}
                
                ordered.each do |link|
                        if link.locale == locale
                                locales[link.name] = link
                        elsif link.locale == nil
                                locales[link.name] ||= link
                        end
                end
                
                ordered = locales.values
        end
        
        # Order by sort key:
        if sort
                # Sort by sort_key, otherwise by title.
                ordered.sort_by!{|link| [link[sort] || sort_default, link.title]}
        end
        
        return ordered
end
metadata(path) click to toggle source
# File lib/utopia/content/links.rb, line 117
def metadata(path)
        @metadata_cache.fetch_or_store(path.to_s) do
                load_metadata(path)
        end
end

Private Instance Methods

load_metadata(path) click to toggle source
# File lib/utopia/content/links.rb, line 142
def load_metadata(path)
        yaml_path = File.join(path, LINKS_YAML)
        
        if File.exist?(yaml_path) && data = YAML.load_file(yaml_path)
                return symbolize_keys(data)
        else
                return {}
        end
end
symbolize_keys(map) click to toggle source
# File lib/utopia/content/links.rb, line 131
def symbolize_keys(map)
        # Second level attributes should be symbolic:
        map.each do |key, info|
                map[key] = info.each_with_object({}) { |(k,v),result| result[k.to_sym] = v }
        end
        
        return map
end