Class: Utopia::Content::Links

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

Overview

Links are essentially a static list of information relating to the structure of the content. They are formed from the ‘links.yaml` file and the actual files 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)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/utopia/content/links.rb', line 93

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.



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

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



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

def ordered
  @ordered
end

#topObject (readonly)

Returns the value of attribute top.



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

def top
  @top
end

Class Method Details

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



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

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

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



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

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



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

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



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

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