Class: Inch::CodeObject::Docstring

Inherits:
Object
  • Object
show all
Defined in:
lib/inch/code_object/docstring.rb

Instance Method Summary collapse

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_examplesObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


8
9
10
# File 'lib/inch/code_object/docstring.rb', line 8

def empty?
  @text.strip.empty?
end

#mentions_parameter?(name) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_examplesObject



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