Class: Bake::Documentation

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/documentation.rb

Overview

Structured access to a set of comment lines.

Constant Summary collapse

DESCRIPTION =
/\A\s*([^@\s].*)?\z/
ATTRIBUTE =
/\A\s*@(?<name>.*?)\s+(?<value>.*?)\z/
PARAMETER =
/\A\s*@param\s+(?<name>.*?)\s+\[(?<type>.*?)\]\s+(?<details>.*?)\z/

Instance Method Summary collapse

Constructor Details

#initialize(comments) ⇒ Documentation

Initialize the documentation with an array of comments.

Parameters:

  • comments (Array(String))

    An array of comment lines.



29
30
31
# File 'lib/bake/documentation.rb', line 29

def initialize(comments)
	@comments = comments
end

Instance Method Details

#attributes {|String| ... } ⇒ Enumerable

The attribute lines of the comment block. e.g. ‘@return [String]`.

Yields:

  • (String)

Returns:

  • (Enumerable)


70
71
72
73
74
75
76
77
78
# File 'lib/bake/documentation.rb', line 70

def attributes
	return to_enum(:attributes) unless block_given?
	
	@comments.each do |comment|
		if match = comment.match(ATTRIBUTE)
			yield match
		end
	end
end

#description {|String| ... } ⇒ Enumerable

The text-only lines of the comment block.

Yields:

  • (String)

Returns:

  • (Enumerable)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bake/documentation.rb', line 39

def description
	return to_enum(:description) unless block_given?
	
	# We track empty lines and only yield a single empty line when there is another line of text:
	gap = false
	
	@comments.each do |comment|
		if match = comment.match(DESCRIPTION)
			if match[1]
				if gap
					yield ""
					gap = false
				end
				
				yield match[1]
			else
				gap = true
			end
		else
			break
		end
	end
end

#parameters {|String| ... } ⇒ Enumerable

The parameter lines of the comment block. e.g. ‘@param value [String] The value.`

Yields:

  • (String)

Returns:

  • (Enumerable)


87
88
89
90
91
92
93
94
95
# File 'lib/bake/documentation.rb', line 87

def parameters
	return to_enum(:parameters) unless block_given?
	
	@comments.each do |comment|
		if match = comment.match(PARAMETER)
			yield match
		end
	end
end