Class: Utopia::Project::Guides
- Inherits:
-
Object
- Object
- Utopia::Project::Guides
- Includes:
- Enumerable
- Defined in:
- lib/utopia/project/guides.rb
Overview
A collection of guides with navigation and lookup capabilities.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Find a guide by name.
-
#each(&block) ⇒ Object
Iterate over all guides.
-
#initialize(base, links) ⇒ Guides
constructor
Initialize the guides collection.
-
#related(guide) ⇒ Object
Get the related guides (previous and next) for the given guide.
-
#to_a ⇒ Object
Get all guides as a sorted array.
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 |
#related(guide) ⇒ Object
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 (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_a ⇒ Object
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 |