Class: Utopia::Content::Links
- Inherits:
-
Object
- Object
- Utopia::Content::Links
- Defined in:
- lib/utopia/content/links.rb
Overview
A collection of links resolved from the filesystem content hierarchy.
Defined Under Namespace
Classes: Resolver
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#file_filter ⇒ Object
readonly
Returns the value of attribute file_filter.
-
#index_filter ⇒ Object
readonly
Returns the value of attribute index_filter.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
-
.for(root, path, locale = nil, fallback: true) ⇒ Object
Build links for the given root and path.
-
.index(root, path, **options) ⇒ Object
Build an index of links for the given path.
Instance Method Summary collapse
-
#for(path, locale = nil, fallback: true) ⇒ Object
Resolve a link for the specified path, which must be a path to a specific link.
-
#index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false) ⇒ Object
Give an index of all links that can be reached from the given path.
-
#initialize(root, extension: XNODE_EXTENSION) ⇒ Links
constructor
Initialize cached link resolution beneath a content root.
-
#links(path) ⇒ Object
Load and cache the link resolver for a content directory.
-
#metadata(path) ⇒ Object
Load and cache metadata for a content directory.
Constructor Details
#initialize(root, extension: XNODE_EXTENSION) ⇒ Links
Initialize cached link resolution beneath a content root.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/utopia/content/links.rb', line 42 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 |
Instance Attribute Details
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
53 54 55 |
# File 'lib/utopia/content/links.rb', line 53 def extension @extension end |
#file_filter ⇒ Object (readonly)
Returns the value of attribute file_filter.
54 55 56 |
# File 'lib/utopia/content/links.rb', line 54 def file_filter @file_filter end |
#index_filter ⇒ Object (readonly)
Returns the value of attribute index_filter.
55 56 57 |
# File 'lib/utopia/content/links.rb', line 55 def index_filter @index_filter end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
116 117 118 |
# File 'lib/utopia/content/links.rb', line 116 def root @root end |
Class Method Details
.for(root, path, locale = nil, fallback: true) ⇒ Object
Build links for the given root and path.
24 25 26 27 |
# File 'lib/utopia/content/links.rb', line 24 def self.for(root, path, locale = nil, fallback: true) warn "Using uncached links metadata!" self.new(root).for(path, locale, fallback: fallback) end |
Instance Method Details
#for(path, locale = nil, fallback: true) ⇒ Object
Resolve a link for the specified path, which must be a path to a specific link.
62 63 64 |
# File 'lib/utopia/content/links.rb', line 62 def for(path, locale = nil, fallback: true) links(path.dirname).lookup(path.last, locale, fallback: fallback) end |
#index(path, name: nil, locale: nil, display: :display, sort: :order, sort_default: 0, directories: true, files: true, virtuals: true, indices: false) ⇒ Object
Give an index of all links that can be reached from the given path.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/utopia/content/links.rb', line 67 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 |
#links(path) ⇒ Object
Load and cache the link resolver for a content directory.
130 131 132 133 134 |
# File 'lib/utopia/content/links.rb', line 130 def links(path) @links_cache.fetch_or_store(path.to_s) do load_links(path) end end |
#metadata(path) ⇒ Object
Load and cache metadata for a content directory.
121 122 123 124 125 |
# File 'lib/utopia/content/links.rb', line 121 def (path) @metadata_cache.fetch_or_store(path.to_s) do (path) end end |