Class: Lazydoc::Method

Inherits:
Comment show all
Defined in:
lib/lazydoc/method.rb

Overview

Method represents a code comment for a standard method definition. Methods give access to the method name, the arguments, and the trailing comment, if present.

sample_method = %Q{
# This is the comment body
def method_name(a, b='default', &c) # trailing comment
end
}

m = Document.new.register(2, Method)
m.resolve(sample_method)
m.method_name          # => "method_name"
m.arguments            # => ["a", "b='default'", "&c"]
m.trailer              # => "trailing comment"
m.to_s                 # => "This is the comment body"

Direct Known Subclasses

Arguments

Constant Summary collapse

METHOD_DEF =

Matches a standard method definition. After the match:

$1:: the method name
$2:: the argument string, which may be parsed by parse_args
/^\s*def (\w+)(.*)$/

Instance Attribute Summary collapse

Attributes inherited from Comment

#content, #document, #line_number, #subject

Instance Method Summary collapse

Methods inherited from Comment

#<<, #append, #comment, #empty?, #parse_down, #parse_up, #prepend, #push, #resolve, #to_s, #trailer, #trim, #unshift, #value, #value=, #wrap

Methods included from Utils

categorize, convert_to_scanner, determine_line_number, match_index, scan, scan_args, scan_index, scan_trailer, skip_quote, split_lines, wrap

Constructor Details

#initialize(*args) ⇒ Method

Returns a new instance of Method.



35
36
37
38
39
# File 'lib/lazydoc/method.rb', line 35

def initialize(*args)
  super
  @method_name = nil
  @arguments = []
end

Instance Attribute Details

#argumentsObject (readonly)

An array of the resolved arguments for the method



33
34
35
# File 'lib/lazydoc/method.rb', line 33

def arguments
  @arguments
end

#method_nameObject (readonly)

The resolved method name



30
31
32
# File 'lib/lazydoc/method.rb', line 30

def method_name
  @method_name
end

Instance Method Details

#subject=(value) ⇒ Object

Overridden to parse and set the method_name, arguments, and trailer in addition to setting the subject.



43
44
45
46
47
48
49
50
51
52
# File 'lib/lazydoc/method.rb', line 43

def subject=(value)
  unless value =~ METHOD_DEF
    raise ArgumentError, "not a method definition: #{value}"
  end
  
  @method_name = $1
  @arguments = scan_args($2)
  
  super
end