Class: Inch::CodeObject::Docstring
- Inherits:
-
Object
- Object
- Inch::CodeObject::Docstring
- Defined in:
- lib/inch/code_object/docstring.rb
Instance Method Summary collapse
- #code_examples ⇒ Object
- #contains_code_example? ⇒ Boolean
- #describes_parameter?(name) ⇒ Boolean
- #describes_return? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(text) ⇒ Docstring
constructor
A new instance of Docstring.
- #mentions_parameter?(name) ⇒ Boolean
- #mentions_return? ⇒ Boolean
- #parse_code_examples ⇒ Object
Constructor Details
#initialize(text) ⇒ Docstring
Returns a new instance of Docstring.
4 5 6 |
# File 'lib/inch/code_object/docstring.rb', line 4 def initialize(text) @text = text.to_s end |
Instance Method Details
#code_examples ⇒ Object
16 17 18 |
# File 'lib/inch/code_object/docstring.rb', line 16 def code_examples @code_examples ||= parse_code_examples end |
#contains_code_example? ⇒ Boolean
12 13 14 |
# File 'lib/inch/code_object/docstring.rb', line 12 def contains_code_example? !code_examples.empty? end |
#describes_parameter?(name) ⇒ Boolean
20 21 22 23 24 |
# File 'lib/inch/code_object/docstring.rb', line 20 def describes_parameter?(name) describe_parameter_regexps(name).any? do |pattern| @text.index(pattern) end end |
#describes_return? ⇒ Boolean
36 37 38 |
# File 'lib/inch/code_object/docstring.rb', line 36 def describes_return? @text.lines.to_a.last =~ /^Returns\ (\w+\s){2,}/ end |
#empty? ⇒ Boolean
8 9 10 |
# File 'lib/inch/code_object/docstring.rb', line 8 def empty? @text.strip.empty? end |
#mentions_parameter?(name) ⇒ Boolean
26 27 28 29 30 |
# File 'lib/inch/code_object/docstring.rb', line 26 def mentions_parameter?(name) mention_parameter_regexps(name).any? do |pattern| @text.index(pattern) end end |
#mentions_return? ⇒ Boolean
32 33 34 |
# File 'lib/inch/code_object/docstring.rb', line 32 def mentions_return? @text.lines.to_a.last =~ /^Returns\ / end |
#parse_code_examples ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/inch/code_object/docstring.rb', line 40 def parse_code_examples code_examples = [] example = nil @text.lines.each_with_index do |line, index| if line =~/^\s*+$/ code_examples << example if example example = [] elsif line =~/^\ {2,}\S+/ example << line if example else code_examples << example if example example = nil end end code_examples << example if example code_examples.delete_if(&:empty?).map(&:join) end |