Class: Utopia::Content::Links::Resolver

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

Overview

Represents a list of Utopia::Content::Link instances relating to the structure of the content. They are formed from the ‘links.yaml` file and the actual directory structure on disk.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links, top = Path.root) ⇒ Resolver

Returns a new instance of Resolver.

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/utopia/content/links.rb', line 154

def initialize(links, top = Path.root)
	raise ArgumentError.new("top path must be absolute") unless top.absolute?
	
	@links = links
	
	@top = top
	
	# top.components.first == '', but this isn't a problem here.
	@path = File.join(links.root, top.components)
	
	@ordered = []
	@named = {}
	
	if File.directory?(@path)
		@metadata = links.(@path)
		
		load_links(@metadata.dup) do |link|
			@ordered << link
			(@named[link.name] ||= []) << link
		end
	else
		@metadata = nil
	end
end

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



181
182
183
# File 'lib/utopia/content/links.rb', line 181

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



180
181
182
# File 'lib/utopia/content/links.rb', line 180

def ordered
  @ordered
end

#topObject (readonly)

Returns the value of attribute top.



179
180
181
# File 'lib/utopia/content/links.rb', line 179

def top
  @top
end

Instance Method Details

#each(locale) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/utopia/content/links.rb', line 187

def each(locale)
	return to_enum(:each, locale) unless block_given?
	
	ordered.each do |links|
		yield links.find{|link| link.locale == locale}
	end
end

#indicesObject



183
184
185
# File 'lib/utopia/content/links.rb', line 183

def indices
	return @ordered.select{|link| link.index?}
end

#lookup(name, locale = nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/utopia/content/links.rb', line 195

def lookup(name, locale = nil)
	# This allows generic links to serve any locale requested.
	if links = @named[name]
		generic_link = nil
		
		links.each do |link|
			if link.locale == locale
				return link
			elsif link.locale.nil?
				generic_link = link
			end
		end
		
		return generic_link
	end
end