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.



44
45
46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



55
56
57
# File 'lib/utopia/content/links.rb', line 55

def extension
  @extension
end

#file_filterObject (readonly)

Returns the value of attribute file_filter.



56
57
58
# File 'lib/utopia/content/links.rb', line 56

def file_filter
  @file_filter
end

#index_filterObject (readonly)

Returns the value of attribute index_filter.



57
58
59
# File 'lib/utopia/content/links.rb', line 57

def index_filter
  @index_filter
end

#rootObject (readonly)

Returns the value of attribute root.



115
116
117
# File 'lib/utopia/content/links.rb', line 115

def root
  @root
end

Class Method Details

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



34
35
36
37
# 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) ⇒ Object



39
40
41
42
# 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

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)



61
62
63
# 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) ⇒ Object

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



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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


123
124
125
126
127
# File 'lib/utopia/content/links.rb', line 123

def links(path)
	@links_cache.fetch_or_store(path.to_s) do
		load_links(path)
	end
end

#metadata(path) ⇒ Object



117
118
119
120
121
# File 'lib/utopia/content/links.rb', line 117

def (path)
	@metadata_cache.fetch_or_store(path.to_s) do
		(path)
	end
end