Class: Utopia::Content::Links

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

Defined Under Namespace

Classes: Resolved

Constant Summary collapse

XNODE_FILTER =
/^(.+)#{Regexp.escape XNODE_EXTENSION}$/
INDEX_XNODE_FILTER =
/^(index(\..+)*)#{Regexp.escape XNODE_EXTENSION}$/
DEFAULT_INDEX_OPTIONS =
{
	:directories => true,
	:files => true,
	:virtuals => true,
	:indices => false,
	:sort => :order,
	:display => :display,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Links

Returns a new instance of Links.



46
47
48
49
50
# File 'lib/utopia/content/links.rb', line 46

def initialize(root)
	@root = root
	
	@cache = Concurrent::Map.new
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



106
107
108
# File 'lib/utopia/content/links.rb', line 106

def root
  @root
end

Class Method Details

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



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

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

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



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

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



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

def for(path, locale = nil)
	links = Resolved.new(self, path.dirname)
	
	links.lookup(path.last, locale)
end

#index(path, **options) ⇒ Object



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
# File 'lib/utopia/content/links.rb', line 67

def index(path, **options)
	options = DEFAULT_INDEX_OPTIONS.merge(options)
	
	ordered = Resolved.new(self, path, options).ordered
	
	# This option filters a link based on the display parameter.
	if display_key = options[:display]
		ordered.reject!{|link| link.info[display_key] == false}
	end
	
	# Named:
	if name = options[:name]
		# We use pattern === name, which matches either the whole string, or matches a regexp.
		ordered.select!{|link| name === link.name}
	end
	
	if locale = options[: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
	
	# Sort:
	if sort_key = options[:sort]
		# Sort by sort_key, otherwise by title.
		ordered.sort_by!{|link| [link[sort_key] || options[:sort_default] || 0, link.title]}
	end
	
	return ordered
end

#metadata(path) ⇒ Object



240
241
242
243
244
# File 'lib/utopia/content/links.rb', line 240

def (path)
	@cache.fetch_or_store(path.to_s) do
		load(path).freeze
	end
end