Class: Utopia::Content::Links

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

Overview

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, top = Path.root, options = DEFAULT_OPTIONS) ⇒ Links

Returns a new instance of Links.

Raises:

  • (ArgumentError)


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 94

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

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



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

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



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

def ordered
  @ordered
end

#topObject (readonly)

Returns the value of attribute top.



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

def top
  @top
end

Class Method Details

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



30
31
32
33
34
# File 'lib/utopia/content/links.rb', line 30

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

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



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

def self.index(root, path, **options)
	options = DEFAULT_INDEX_OPTIONS.merge(options)
	
	ordered = self.new(root, 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]
		ordered.select!{|link| link.name[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

Instance Method Details

#each(locale) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/utopia/content/links.rb', line 119

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

#lookup(name, locale = nil) ⇒ Object



127
128
129
130
131
132
# File 'lib/utopia/content/links.rb', line 127

def lookup(name, locale = nil)
	# This allows generic links to serve any locale requested.
	if links = @named[name]
		links.find{|link| link.locale == locale} || links.find{|link| link.locale == nil}
	end
end