Class: Utopia::Project::Guides

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/utopia/project/guides.rb

Overview

A collection of guides with navigation and lookup capabilities.

Instance Method Summary collapse

Constructor Details

#initialize(base, links) ⇒ Guides

Initialize the guides collection.



17
18
19
20
# File 'lib/utopia/project/guides.rb', line 17

def initialize(base, links)
	@base = base
	@links = links
end

Instance Method Details

#[](name) ⇒ Object

Find a guide by name.



47
48
49
# File 'lib/utopia/project/guides.rb', line 47

def [](name)
	to_a.find{|guide| guide.name == name}
end

#each(&block) ⇒ Object

Iterate over all guides.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/utopia/project/guides.rb', line 26

def each(&block)
	return to_enum(:each) unless block_given?
	
	@links.index("/guides").each do |link|
		guide_path = File.join(@base.root, link.path)
		
		next unless File.directory?(guide_path)
		
		yield Guide.new(@base, guide_path, link.info)
	end
end

Get the related guides (previous and next) for the given guide.



54
55
56
57
58
59
60
61
62
# File 'lib/utopia/project/guides.rb', line 54

def related(guide)
	index = to_a.index{|g| g.name == guide.name}
	return [nil, nil] unless index
	
	previous_guide = index > 0 ? to_a[index - 1] : nil
	next_guide = index < to_a.size - 1 ? to_a[index + 1] : nil
	
	[previous_guide, next_guide]
end

#to_aObject

Get all guides as a sorted array.



40
41
42
# File 'lib/utopia/project/guides.rb', line 40

def to_a
	@array ||= super.sort
end