123456789_123456789_123456789_123456789_123456789_

Class: YARD::Handlers::Ruby::ConstantHandler

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: YARD::Handlers::Ruby::Base
Defined in: lib/yard/handlers/ruby/constant_handler.rb

Overview

Handles any constant assignment

Constant Summary

::YARD::CodeObjects - Included

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

Class Attribute Summary

::YARD::Handlers::Base - Inherited

.namespace_only

Declares that the handler should only be called when inside a ::YARD::CodeObjects::NamespaceObject, not a method body.

.namespace_only?

Class Method Summary

Base - Inherited

.handles?,
.meta_type

Matcher for handling a node with a specific meta-type.

.method_call

Matcher for handling any type of method call.

::YARD::Parser::Ruby - Extended

s

Builds and s-expression by creating AstNode objects with the type provided by the first argument.

::YARD::Handlers::Base - Inherited

.clear_subclasses

Clear all registered subclasses.

.handlers,
.handles

Declares the statement type which will be processed by this handler.

.handles?

This class is implemented by Base and Legacy::Base.

.in_file

Declares that a handler should only be called when inside a filename by its basename or a regex match for the full path.

.inherited, .matches_file?, .new,
.process

Generates a #process method, equivalent to +def process; ...

.subclasses

Returns all registered handler subclasses.

Instance Attribute Summary

::YARD::Handlers::Base - Inherited

#extra_state

Share state across different handlers inside of a file.

#globals

::YARD::Handlers can share state for the entire post processing stage through this attribute.

#namespace, #namespace=, #owner, #owner=, #parser, #scope, #scope=, #statement, #visibility, #visibility=

Instance Method Summary

StructHandlerMethods - Included

#add_reader_tags

Creates the auto-generated docstring for the getter method of a struct's member.

#add_writer_tags

Creates the auto-generated docstring for the setter method of a struct's member.

#create_attributes

Creates the given member methods and attaches them to the given ClassObject.

#create_class

Creates and registers a class object with the given name and superclass name.

#create_member_method?

Determines whether to create an attribute method based on the class's tags.

#create_reader

Creates the getter (reader) method and attaches it to the class as an attribute.

#create_writer

Creates the setter (writer) method and attaches it to the class as an attribute.

#member_tag_for_member

Extracts the user's defined @member tag for a given class and its member.

#members_from_tags

Retrieves all members defined in @attr* tags.

#return_type_from_tag

Gets the return type for the member in a nicely formatted string.

Base - Inherited

::YARD::Parser::Ruby - Included

#s

Builds and s-expression by creating AstNode objects with the type provided by the first argument.

::YARD::Handlers::Base - Inherited

#abort!

Aborts a handler by raising ::YARD::Handlers::HandlerAborted.

#call_params, #caller_method,
#ensure_loaded!

Ensures that a specific object has been parsed and loaded into the registry.

#parse_block

Parses the semantic "block" contained in the statement node.

#process

The main handler method called by the parser on a statement that matches the handles declaration.

#push_state

Executes a given block with specific state values for #owner, #namespace and #scope.

#register

Do some post processing on a list of code objects.

#register_docstring

Registers any docstring found for the object and expands macros.

#register_dynamic

Registers the object as dynamic if the object is defined inside a method or block (owner != namespace).

#register_ensure_loaded

Ensures that the object's namespace is loaded before attaching it to the namespace.

#register_file_info

Registers the file/line of the declaration with the object.

#register_group

Registers the object as being inside a specific group.

#register_module_function

Registers the same method information on the module function, if the object was defined as a module function.

#register_source,
#register_transitive_tags

Registers any transitive tags from the namespace on the object.

#register_visibility

Registers visibility on a method object.

Constructor Details

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

Instance Method Details

#extract_parameters(superclass) ⇒ Array<String> (private)

Extract the parameters from the Struct.new AST node, returning them as a list of strings

Parameters:

  • superclass (MethodCallNode)

    the AST node for the Struct.new call

Returns:

  • (Array<String>)

    the member names to generate methods for

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/constant_handler.rb', line 49

def extract_parameters(superclass)
  return [] unless superclass.parameters
  members = superclass.parameters.select {|x| x && x.type == :symbol_literal }
  members.map! {|x| x.source.strip[1..-1] }
  members
end

#processvoid

This method returns an undefined value.

Main processing callback

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/constant_handler.rb', line 8

process do
  if statement[1].call? && statement[1][0][0] == s(:const, "Struct") &&
     statement[1][2] == s(:ident, "new")
    process_structclass(statement)
  elsif statement[0].type == :var_field && statement[0][0].type == :const
    process_constant(statement)
  elsif statement[0].type == :const_path_field
    process_constant(statement)
  end
end

#process_constant(statement) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/constant_handler.rb', line 21

def process_constant(statement)
  name = statement[0].source
  value = statement[1].source
  obj = P(namespace, name)
  if obj.is_a?(NamespaceObject) && obj.namespace == namespace
    raise YARD::Parser::UndocumentableError, "constant for existing #{obj.type} #{obj}"
  else
    ensure_loaded! obj.parent
    register ConstantObject.new(namespace, name) {|o| o.source = statement; o.value = value.strip }
  end
end

#process_structclass(statement) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/constant_handler.rb', line 33

def process_structclass(statement)
  lhs = statement[0][0]
  if lhs.type == :const
    klass = create_class(lhs[0], P(:Struct))
    create_attributes(klass, extract_parameters(statement[1]))
    parse_block(statement[1].block[1], :namespace => klass) unless statement[1].block.nil?
  else
    raise YARD::Parser::UndocumentableError, "Struct assignment to #{statement[0].source}"
  end
end