yard-forwardable_def_delegators_handler.rb 1.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
class ForwardableDefDelegatorsHandler < YARD::Handlers::Ruby::Base
  handles method_call(:def_delegators)
  namespace_only

  def process
    params = validated_attribute_names(statement.parameters(false))
    accessor = params.shift
    params.each do |param|
      object = YARD::CodeObjects::MethodObject.new(namespace, param)
      object.docstring = "Forwarded to +#{accessor}+"
    end
  end

  protected

  # Strips out any non-essential arguments from the attr statement.
  #
  # @param [Array<Parser::Ruby::AstNode>] params a list of the parameters
  #   in the attr call.
  # @return [Array<String>] the validated attribute names
  # @raise [Parser::UndocumentableError] if the arguments are not valid.
  def validated_attribute_names(params)
    params.map do |obj|
      case obj.type
      when :symbol_literal
        obj.jump(:ident, :op, :kw, :const).source
      when :string_literal
        obj.jump(:string_content).source
      else
        raise YARD::Parser::UndocumentableError, obj.source
      end
    end
  end
end