Class: Utopia::Content::Links
- Inherits:
-
Object
- Object
- Utopia::Content::Links
- Defined in:
- lib/utopia/content/links.rb
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
Instance Method Summary collapse
-
#for(path, locale = nil) ⇒ 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
A new instance of Links.
- #links(path) ⇒ Object
- #metadata(path) ⇒ Object
Constructor Details
#initialize(root, extension: XNODE_EXTENSION) ⇒ Links
Returns a new instance of Links.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/utopia/content/links.rb', line 27 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.
38 39 40 |
# File 'lib/utopia/content/links.rb', line 38 def extension @extension end |
#file_filter ⇒ Object (readonly)
Returns the value of attribute file_filter.
39 40 41 |
# File 'lib/utopia/content/links.rb', line 39 def file_filter @file_filter end |
#index_filter ⇒ Object (readonly)
Returns the value of attribute index_filter.
40 41 42 |
# File 'lib/utopia/content/links.rb', line 40 def index_filter @index_filter end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
98 99 100 |
# File 'lib/utopia/content/links.rb', line 98 def root @root end |
Class Method Details
Instance Method Details
#for(path, locale = nil) ⇒ Object
Resolve a link for the specified path, which must be a path to a specific link. for(Path)
44 45 46 |
# File 'lib/utopia/content/links.rb', line 44 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) ⇒ Object
Give an index of all links that can be reached from the given path.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/utopia/content/links.rb', line 49 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
106 107 108 109 110 |
# File 'lib/utopia/content/links.rb', line 106 def links(path) @links_cache.fetch_or_store(path.to_s) do load_links(path) end end |
#metadata(path) ⇒ Object
100 101 102 103 104 |
# File 'lib/utopia/content/links.rb', line 100 def (path) @metadata_cache.fetch_or_store(path.to_s) do (path) end end |