Class: Utopia::Project::Guide

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/project/guide.rb

Overview

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

Constant Summary collapse

README =
"README.md"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, root) ⇒ Guide

Initialize the example with the given root path.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/utopia/project/guide.rb', line 35

def initialize(base, root)
	@base = base
	@root = root
	
	@documentation = nil
	
	@document = nil
	@title = nil
	@description = nil
	
	self.document
end

Instance Attribute Details

#baseObject

The base instance of the project this example is loaded from.



93
94
95
# File 'lib/utopia/project/guide.rb', line 93

def base
  @base
end

#descriptionObject (readonly)

The description from the first paragraph in the README.



50
51
52
# File 'lib/utopia/project/guide.rb', line 50

def description
  @description
end

#rootObject (readonly)

The file-system path to the root of the project.



97
98
99
# File 'lib/utopia/project/guide.rb', line 97

def root
  @root
end

Instance Method Details

#documentObject

The document for the README, if one exists.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/utopia/project/guide.rb', line 68

def document
	if self.readme?
		@document ||= self.readme_document.tap do |document|
			root = document.root
			if element = root.children.first
				if element.type == :header
					@title = element.children.first.value
					
					# Remove the title:
					root.children.shift
					
					# Remove any blank lines:
					root.children.shift while root.children.first&.type == :blank
					
					# Read the description:
					root.children.first.options[:encoding] = root.options[:encoding]
					@description = Kramdown::Converter::Kramdown.convert(root.children.first).first
				end
			end
		end
	end
end

#documentationObject

The best documentation, extracted from the source files of the guide.



119
120
121
# File 'lib/utopia/project/guide.rb', line 119

def documentation
	@documentation ||= self.best_documentation
end

#filesObject

All files associated with this guide.



125
126
127
# File 'lib/utopia/project/guide.rb', line 125

def files
	Dir.glob(File.expand_path("*", @root))
end

#hrefObject

The hypertext reference to this guide.



113
114
115
# File 'lib/utopia/project/guide.rb', line 113

def href
	"/guides/#{self.name}/index"
end

#nameObject

The name of the guide.



101
102
103
# File 'lib/utopia/project/guide.rb', line 101

def name
	File.basename(@root)
end

#readme?Boolean

Does a README file exist for this guide?

Returns:

  • (Boolean)


62
63
64
# File 'lib/utopia/project/guide.rb', line 62

def readme?
	File.exist?(readme_path)
end

#readme_pathObject

The path to the README file for the guide.



56
57
58
# File 'lib/utopia/project/guide.rb', line 56

def readme_path
	File.expand_path(README, @root)
end

#sourcesObject

All the source files associated with this guide.



133
134
135
136
137
138
139
140
141
# File 'lib/utopia/project/guide.rb', line 133

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

#titleObject

The title of the guide.



107
108
109
# File 'lib/utopia/project/guide.rb', line 107

def title
	@title || Trenni::Strings.to_title(self.name)
end