Class: Utopia::Content::Links

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#extensionObject (readonly)

Returns the value of attribute extension.



53
54
55
# File 'lib/utopia/content/links.rb', line 53

def extension
  @extension
end

#file_filterObject (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_filterObject (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

#rootObject (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

.index(root, path, **options) ⇒ Object

Build an index of links for the given path.



34
35
36
37
# File 'lib/utopia/content/links.rb', line 34

def self.index(root, path, **options)
	warn "Using uncached links metadata!"
	self.new(root).index(path, **options)
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

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