# Class: YARD::Tags::MethodDirective

Inherits:
  Directive
  - Object
  - Directive
  - YARD::Tags::MethodDirective
  show all

Defined in:
  lib/yard/tags/directives.rb

## Overview

Note:

This directive should only be used if there is no explicit declaration for the method in any source files (i.e., the method is declared dynamically via meta-programming). In all other cases, add documentation to the method definition itself.

Note:

For backwards compatibility support, you do not need to indent the method’s docstring text. If a `@!method` directive is seen with no indented block, the entire docstring is used as the new method’s docstring text.

Defines a method object with a given method signature, using indented block data as the method’s docstring. The signature is similar to the @overload tag. The comment containing this directive does not need to be attached to any source, but if it is, that source code will be used as the method’s source.

To define an attribute method, see @!attribute

#### Examples:

#####

Defining a simple method

    # @!method quit(username, message = "Quit")
    #   Sends a quit message to the server for a +username+.
    #   @param [String] username the username to quit
    #   @param [String] message the quit message
    quit_message_method

#####

Attaching multiple methods to the same source

    # @!method method1
    # @!method method2
    create_methods :method1, :method2

See Also:

- @!attribute

Since:

- 0.7.0

## Direct Known Subclasses

AttributeDirective

## Parser callbacks collapse

SCOPE_MATCH =

Since:

- 0.7.0

      /\A\s*self\s*\.\s*/

## Parser callbacks collapse

- #after_parse ⇒ Object
- #call ⇒ Object
- #create_object ⇒ Object protected
- #method_name ⇒ Object protected
- #method_signature ⇒ Object protected
- #sanitized_tag_signature ⇒ Object protected
- #use_indented_text ⇒ Object protected

## Constructor Details

This class inherits a constructor from YARD::Tags::Directive

## Instance Method Details

### #after_parse ⇒ `Object`

Since:

- 0.7.0

    372
    373
    374
    375
    376

    # File 'lib/yard/tags/directives.rb', line 372

    def after_parse
      return unless handler
      use_indented_text
      create_object
    end

### #call ⇒ `Object`

Since:

- 0.7.0

    370

    # File 'lib/yard/tags/directives.rb', line 370

    def call; end

### #create_object ⇒ `Object` (protected)

Since:

- 0.7.0

    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432

    # File 'lib/yard/tags/directives.rb', line 412

    def create_object
      name = method_name
      scope = parser.state.scope || handler.scope
      visibility = parser.state.visibility || handler.visibility
      ns = CodeObjects::NamespaceObject === object ? object : handler.namespace
      obj = CodeObjects::MethodObject.new(ns, name, scope)
      handler.register_file_info(obj)
      handler.register_source(obj)
      handler.register_visibility(obj, visibility)
      handler.register_group(obj)
      obj.signature = method_signature
      obj.parameters = OverloadTag.new(:overload, method_signature).parameters
      obj.docstring = Docstring.new!(parser.text, parser.tags, obj,
        parser.raw_text, parser.reference)
      handler.register_module_function(obj)
      old_obj = parser.object
      parser.object = obj
      parser.post_process
      parser.object = old_obj
      obj
    end

### #method_name ⇒ `Object` (protected)

Since:

- 0.7.0

    380
    381
    382
    383
    384
    385
    386
    387

    # File 'lib/yard/tags/directives.rb', line 380

    def method_name
      sig = sanitized_tag_signature
      if sig && sig =~ /^#{CodeObjects::METHODNAMEMATCH}(\s|\(|$)/
        sig[/\A\s*([^\(; \t]+)/, 1]
      else
        handler.call_params.first
      end
    end

### #method_signature ⇒ `Object` (protected)

Since:

- 0.7.0

    389
    390
    391

    # File 'lib/yard/tags/directives.rb', line 389

    def method_signature
      "def #{sanitized_tag_signature || method_name}"
    end

### #sanitized_tag_signature ⇒ `Object` (protected)

Since:

- 0.7.0

    393
    394
    395
    396
    397
    398
    399
    400

    # File 'lib/yard/tags/directives.rb', line 393

    def sanitized_tag_signature
      if tag.name && tag.name =~ SCOPE_MATCH
        parser.state.scope = :class
        $'
      else
        tag.name
      end
    end

### #use_indented_text ⇒ `Object` (protected)

Since:

- 0.7.0

    402
    403
    404
    405
    406
    407
    408
    409
    410

    # File 'lib/yard/tags/directives.rb', line 402

    def use_indented_text
      return if tag.text.empty?
      handler = parser.handler
      object = parser.object
      self.parser = parser.class.new(parser.library)
      parser.state.inside_directive = true
      parser.parse(tag.text, object, handler)
      parser.state.inside_directive = false
    end