class Utopia::Project::Guide

Provides structured access to a directory which contains documentation and source code to explain a specific process.

Constants

README

Attributes

base[R]

The base instance of the project this example is loaded from. @attribute [Base]

description[R]

The description from the first paragraph in the README. @attribute [String | Nil]

root[R]

The file-system path to the root of the project. @attribute [String]

Public Class Methods

new(base, root) click to toggle source

Initialize the example with the given root path. @parameter base [Base] The base instance for the project. @parameter root [String] The file-system path to the root of the example.

# File lib/utopia/project/guide.rb, line 34
def initialize(base, root)
        @base = base
        @root = root
        
        @documentation = nil
        
        @document = nil
        @title = nil
        @description = nil
        
        self.document
end

Public Instance Methods

document() click to toggle source

The document for the README, if one exists.

# File lib/utopia/project/guide.rb, line 66
def document
        if self.readme?
                @document ||= self.readme_document.tap do |document|
                        child = document.first_child
                        
                        if child.type == :header
                                @title = child.first_child.string_content
                                
                                @description = child.next
                                child.delete
                        end
                end
        end
end
documentation() click to toggle source

The best documentation, extracted from the source files of the guide. @returns [Decode::Documentation]

# File lib/utopia/project/guide.rb, line 109
def documentation
        @documentation ||= self.best_documentation
end
files() click to toggle source

All files associated with this guide. @returns [Array(String)] The file-system paths.

# File lib/utopia/project/guide.rb, line 115
def files
        Dir.glob(File.expand_path("*", @root))
end
href() click to toggle source

The hypertext reference to this guide. @returns [String]

# File lib/utopia/project/guide.rb, line 103
def href
        "/guides/#{self.name}/index"
end
name() click to toggle source

The name of the guide. @returns [String]

# File lib/utopia/project/guide.rb, line 91
def name
        File.basename(@root)
end
readme?() click to toggle source

Does a README file exist for this guide? @returns [Boolean]

# File lib/utopia/project/guide.rb, line 61
def readme?
        File.exist?(readme_path)
end
readme_path() click to toggle source

The path to the README file for the guide. @returns [String] The file-system path.

# File lib/utopia/project/guide.rb, line 55
def readme_path
        File.expand_path(README, @root)
end
sources() { |source| ... } click to toggle source

All the source files associated with this guide. @yields {|source| …} If a block is given.

@parameter source [Decode::Source]

@returns [Enumerator(Decode::Source)] If no block is given.

# File lib/utopia/project/guide.rb, line 123
def sources
        return to_enum(:sources) unless block_given?
        
        files.each do |path|
                if source = @base.index.languages.source_for(path)
                        yield source
                end
        end
end
title() click to toggle source

The title of the guide. @returns [String]

# File lib/utopia/project/guide.rb, line 97
def title
        @title || Trenni::Strings.to_title(self.name)
end

Private Instance Methods

best_documentation() click to toggle source
# File lib/utopia/project/guide.rb, line 141
def best_documentation
        if source = sources.first
                if segment = source.segments.first
                        return segment.documentation
                end
        end
end
readme_document() click to toggle source
# File lib/utopia/project/guide.rb, line 135
def readme_document
        content = File.read(self.readme_path)
        
        return @base.document(content)
end