Class: Utopia::Content::Links

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/links.rb

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

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

#extensionObject (readonly)

Returns the value of attribute extension.



38
39
40
# File 'lib/utopia/content/links.rb', line 38

def extension
  @extension
end

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

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

.for(root, path, locale = nil) ⇒ Object



17
18
19
20
# File 'lib/utopia/content/links.rb', line 17

def self.for(root, path, locale = nil)
	warn "Using uncached links metadata!"
	self.new(root).for(path, locale)
end

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



22
23
24
25
# File 'lib/utopia/content/links.rb', line 22

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) ⇒ 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


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