Class: YARD::Docstring

Inherits:
String
  • Object
show all
Defined in:
lib/yard/docstring.rb

Overview

A documentation string, or “docstring” for short, encapsulates the comments and metadata, or “tags”, of an object. Meta-data is expressed in the form @tag VALUE, where VALUE can span over multiple lines as long as they are indented. The following @example tag shows how tags can be indented:

  # @example My example
  #   a = "hello world"
  #   a.reverse
  # @version 1.0

Tags can be nested in a documentation string, though the Tags::Tag itself is responsible for parsing the inner tags.

Constant Summary

META_MATCH = Matches a tag at the start of a comment line.
/^@([a-z_]+)(?:\s+(.*))?$/i

Instance Attribute Summary

Instance Method Summary

Methods inherited from String

#camelcase, #shell_split, #underscore

Constructor Details

- (Docstring) initialize(content = '', object = nil)

Creates a new docstring with the raw contents attached to an optional object.

Examples:

  Docstring.new("hello world\n@return Object return", someobj)

Parameters:

  • (String) content (defaults to: '') — the raw comments to be parsed into a docstring and associated meta-data.
  • (CodeObjects::Base) object (defaults to: nil) — an object to associate the docstring with.


41
42
43
44
45
# File 'lib/yard/docstring.rb', line 41

def initialize(content = '', object = nil)
  @object = object
  
  self.all = content
end

Instance Attribute Details

- (String) all

The raw documentation (including raw tag text)

Returns:

  • (String) — the raw documentation (including raw tag text)


26
27
28
# File 'lib/yard/docstring.rb', line 26

def all
  @all
end

- (Range) line_range

Line range in the #object’s file where the docstring was parsed from

Returns:

  • (Range) — line range in the #object’s file where the docstring was parsed from


23
24
25
# File 'lib/yard/docstring.rb', line 23

def line_range
  @line_range
end

- (CodeObjects::Base) object

The object that owns the docstring.

Returns:



20
21
22
# File 'lib/yard/docstring.rb', line 20

def object
  @object
end

- (Array<Tags::RefTag>) ref_tags (readonly)

The list of reference tags

Returns:



17
18
19
# File 'lib/yard/docstring.rb', line 17

def ref_tags
  @ref_tags
end

Instance Method Details

- (Object) add_tag(*tags)

Adds a tag or reftag object to the tag list

Parameters:



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/yard/docstring.rb', line 88

def add_tag(*tags)
  tags.each_with_index do |tag, i|
    case tag
    when Tags::Tag
      tag.object = object
      @tags << tag
    when Tags::RefTag
      @ref_tags << tag
    else
      raise ArgumentError, "expected Tag or RefTag, got #{tag.class} (at index #{i})"
    end
  end
end

- (Boolean) blank?

Returns true if the docstring has no content

Returns:

  • (Boolean) — whether or not the docstring has content


137
138
139
# File 'lib/yard/docstring.rb', line 137

def blank?
  empty? && @tags.empty? && @ref_tags.empty?
end

- (Boolean) has_tag?(name)

Returns true if at least one tag by the name name was declared

Parameters:

  • (String) name — the tag name to search for

Returns:

  • (Boolean) — whether or not the tag name was declared


130
131
132
# File 'lib/yard/docstring.rb', line 130

def has_tag?(name)
  tags.any? {|tag| tag.tag_name.to_s == name.to_s }
end

- (Fixnum) line

The first line of the #line_range.

Returns:



57
58
59
# File 'lib/yard/docstring.rb', line 57

def line
  line_range.first
end

- (Object) replace(content) Also known as: all=

Replaces the docstring with new raw content. Called by #all=.

Parameters:

  • (String) content — the raw comments to be parsed


49
50
51
52
53
# File 'lib/yard/docstring.rb', line 49

def replace(content)
  @tags, @ref_tags = [], []
  @all = content
  super parse_comments(content)
end

- (String) summary

Gets the first line of a docstring to the period or the first paragraph.

Returns:

  • (String) — The first line or paragraph of the docstring; always ends with a period.


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yard/docstring.rb', line 63

def summary
  return @summary if @summary
  open_parens = ['{', '(', '[']
  close_parens = ['}', ')', ']']
  num_parens = 0
  idx = length.times do |index|
    case self[index, 1]
    when ".", "\r", "\n"
      next_char = self[index + 1, 1].to_s
      if num_parens == 0 && next_char =~ /^\s*$/
        break index - 1
      end
    when "{", "(", "["
      num_parens += 1
    when "}", ")", "]"
      num_parens -= 1
    end
  end
  @summary = self[0..idx]
  @summary += '.' unless @summary.empty?
  @summary
end

- (Tags::Tag) tag(name)

Convenience method to return the first tag object in the list of tag objects of that name

Examples:

  doc = Docstring.new("@return zero when nil")
  doc.tag(:return).text  # => "zero when nil"

Parameters:

  • (#to_s) name — the tag name to return data for

Returns:



111
112
113
# File 'lib/yard/docstring.rb', line 111

def tag(name)
  tags.find {|tag| tag.tag_name.to_s == name.to_s }
end

- (Array<Tags::Tag>) tags(name = nil)

Returns a list of tags specified by name or all tags if name is not specified.

Parameters:

  • (#to_s) name (defaults to: nil) — the tag name to return data for, or nil for all tags

Returns:



119
120
121
122
123
# File 'lib/yard/docstring.rb', line 119

def tags(name = nil)
  list = @tags + convert_ref_tags
  return list unless name
  list.select {|tag| tag.tag_name.to_s == name.to_s }
end