Class: YARD::Handlers::Ruby::AttributeHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/handlers/ruby/attribute_handler.rb

Overview

Handles attr_* statements in modules/classes

Constant Summary

Constant Summary

Constants included from CodeObjects

CodeObjects::BUILTIN_ALL, CodeObjects::BUILTIN_CLASSES, CodeObjects::BUILTIN_EXCEPTIONS, CodeObjects::BUILTIN_EXCEPTIONS_HASH, CodeObjects::BUILTIN_MODULES, CodeObjects::CONSTANTMATCH, CodeObjects::CSEP, CodeObjects::CSEPQ, CodeObjects::ISEP, CodeObjects::ISEPQ, CodeObjects::METHODMATCH, CodeObjects::METHODNAMEMATCH, CodeObjects::NAMESPACEMATCH, CodeObjects::NSEP, CodeObjects::NSEPQ

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Parser::Ruby

#s

Constructor Details

This class inherits a constructor from YARD::Handlers::Base

Instance Attribute Details

- (Object) extra_state (readonly) Originally defined in class Base

Returns the value of attribute extra_state

- (Object) globals (readonly) Originally defined in class Base

Returns the value of attribute globals

- (Object) namespace Originally defined in class Base

Returns the value of attribute namespace

- (Object) owner Originally defined in class Base

Returns the value of attribute owner

- (Processor) parser (readonly) Originally defined in class Base

Returns the processor object that manages all global state during handling.

Returns:

  • (Processor)

    the processor object that manages all global state during handling.

- (Object) scope Originally defined in class Base

Returns the value of attribute scope

- (Object) statement (readonly) Originally defined in class Base

Returns the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

Returns:

  • (Object)

    the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

- (Object) visibility Originally defined in class Base

Returns the value of attribute visibility

Instance Method Details

- (void) process

This method returns an undefined value.

Main processing callback



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yard/handlers/ruby/attribute_handler.rb', line 9

process do
  return if statement.type == :var_ref || statement.type == :vcall
  read, write = true, false
  params = statement.parameters(false).dup

  # Change read/write based on attr_reader/writer/accessor
  case statement.method_name(true)
  when :attr
    # In the case of 'attr', the second parameter (if given) isn't a symbol.
    if params.size == 2
      write = true if params.pop == s(:var_ref, s(:kw, "true"))
    end
  when :attr_accessor
    write = true
  when :attr_reader
    # change nothing
  when :attr_writer
    read, write = false, true
  end

  # Add all attributes
  validated_attribute_names(params).each do |name|
    namespace.attributes[scope][name] ||= SymbolHash[:read => nil, :write => nil]

    # Show their methods as well
    {:read => name, :write => "#{name}="}.each do |type, meth|
      if (type == :read ? read : write)
        o = MethodObject.new(namespace, meth, scope)
        if type == :write
          o.parameters = [['value', nil]]
          src = "def #{meth}(value)"
          full_src = "#{src}\n  @#{name} = value\nend"
          doc = "Sets the attribute #{name}\n@param value the value to set the attribute #{name} to."
        else
          src = "def #{meth}"
          full_src = "#{src}\n  @#{name}\nend"
          doc = "Returns the value of attribute #{name}"
        end
        o.source ||= full_src
        o.signature ||= src
        register(o)
        o.docstring = doc if o.docstring.blank?(false)

        # Regsiter the object explicitly
        namespace.attributes[scope][name][type] = o
      elsif obj = namespace.children.find {|o| o.name == meth.to_sym && o.scope == scope }
        # register an existing method as attribute
        namespace.attributes[scope][name][type] = obj
      end
    end
  end
end

- (Array<String>) validated_attribute_names(params) (protected)

Strips out any non-essential arguments from the attr statement.

Parameters:

Returns:

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/yard/handlers/ruby/attribute_handler.rb', line 70

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