Module: YARD::Handlers::Ruby::DecoratorHandlerMethods

Included in:
ModuleFunctionHandler, PrivateClassMethodHandler, PublicClassMethodHandler, VisibilityHandler
Defined in:
lib/yard/handlers/ruby/decorator_handler_methods.rb

Overview

Helper methods to assist with processing decorators.

Instance Method Summary collapse

Instance Method Details

#process_decorator(*nodes, opts = {}) {|method, node, name| ... } ⇒ Array<Hash>

Takes care of parsing method definitions passed to decorators as parameters, as well as parsing chained decorators.

Use this in a handler’s process block.

Examples:

Basic Usage

# Simply pass the method docs through to the method definition.
process do
  process_decorator
end

Setting a method’s visibility to private.

process do
  process_decorator :scope => :class do |method|
    method.visibility = :private if method.respond_to? :visibility
  end
end

Parameters:

  • nodes (YARD::Parser::Ruby::AstNode)

    AST nodes that refer to decorated methods, like indexes of statement.parameter. Defaults to all parameters. Pass nil to specify zero parameters.

Options Hash (opts):

  • :scope (:instance, :class) — default: :instance

    Scope to use for each MethodObject.

  • :transfer_docstring (true, false)

    Set false to disable transferring the decorator docstring to method definitions passed to the decorator as parameters.

  • :transfer_source (true, false)

    Set false to disable transferring the decorator source code string to method definitions passed to the decorator as parameters.

Yield Parameters:

Returns:

  • (Array<Hash>)

    Array of hashes containing :method, :node, :name. See yield params.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yard/handlers/ruby/decorator_handler_methods.rb', line 43

def process_decorator(*nodes, &block)
  opts = nodes.last.is_a?(Hash) ? nodes.pop : {}

  all_nodes = statement.parameters.select do |p|
    p.is_a? YARD::Parser::Ruby::AstNode
  end

  # Parse decorator parameters (decorator chain).
  all_nodes.each do |param|
    parse_block param if param.call? || param.def?
  end

  selected_nodes =
    if nodes.empty?
      all_nodes
    elsif nodes.count == 1 && nodes.first.nil?
      []
    else
      nodes
    end

  decorated_methods = selected_nodes.map do |param|
    process_decorator_parameter param, opts, &block
  end.flatten

  # Store method nodes in decorator node.
  statement.define_singleton_method :decorators do
    decorated_methods.map {|h| h[:node] }
  end

  decorated_methods
end