123456789_123456789_123456789_123456789_123456789_

Class: YARD::Handlers::Ruby::Legacy::ClassHandler

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

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

::YARD::Parser::Ruby::Legacy::RubyToken - Included

EXPR_ARG, EXPR_BEG, EXPR_CLASS, EXPR_DOT, EXPR_END, EXPR_FNAME, EXPR_MID, NEWLINE_TOKEN, TkReading2Token, TkSymbol2Token, TokenDefinitions

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

::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 ::YARD::Handlers::Ruby::Base and 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

::YARD::Handlers::Ruby::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

#call_params, #caller_method,
#parse_block

Parses a statement's block with a set of state values.

#extract_method_details

Extracts method information for macro expansion only.

#tokval

The string value of a token.

#tokval_list

Returns a list of symbols or string values from a statement.

::YARD::Parser::Ruby::Legacy::RubyToken - Included

::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

#create_struct_superclass(superclass, superclass_def) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/legacy/class_handler.rb', line 74

def create_struct_superclass(superclass, superclass_def)
  return if superclass == "Struct"
  the_super = register ClassObject.new(P("Struct"), superclass[8..-1]) do |o|
    o.superclass = "Struct"
  end
  parse_struct_subclass(the_super, superclass_def)
  the_super
end

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

Extracts the parameter list from the Struct.new declaration and returns it formatted as a list of member names. Expects the user will have used symbols to define the struct member names

Parameters:

  • superstring (String)

    the string declaring the superclass

Returns:

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/legacy/class_handler.rb', line 69

def extract_parameters(superstring)
  paramstring = superstring.match(/\A(O?Struct)\.new\((.*?)\)/)[2]
  paramstring.split(",").select {|x| x.strip[0, 1] == ":" }.map {|x| x.strip[1..-1] } # the 1..-1 chops the leading :
end

#parse_struct_subclass(klass, superclass_def) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/legacy/class_handler.rb', line 95

def parse_struct_subclass(klass, superclass_def)
  # Bounce if there's no parens
  return unless superclass_def =~ /O?Struct\.new\((.*?)\)/
  members = extract_parameters(superclass_def)
  create_attributes(klass, members)
end

#parse_superclass(superclass) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/legacy/class_handler.rb', line 102

def parse_superclass(superclass)
  case superclass
  when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,
       /\A(Struct|OStruct)\.new/,
       /\ADelegateClass\((.+?)\)\s*\Z/,
       /\A(#{NAMESPACEMATCH})\(/
    $1
  when "self"
    namespace.path
  end
end

#processvoid

This method returns an undefined value.

Main processing callback

[ GitHub ]

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

process do
  if statement.tokens.to_s =~ /^class\s+(#{NAMESPACEMATCH})\s*(?:<\s*(.+)|\Z)/m
    classname = $1
    superclass_def = $2
    superclass = parse_superclass($2)
    classname = classname.gsub(/\s/, '')
    if superclass == "Struct"
      is_a_struct = true
      superclass = struct_superclass_name(superclass_def)
      create_struct_superclass(superclass, superclass_def)
    end
    undocsuper = superclass_def && superclass.nil?

    klass = register ClassObject.new(namespace, classname) do |o|
      o.superclass = superclass if superclass
      o.superclass.type = :class if o.superclass.is_a?(Proxy)
    end
    if is_a_struct
      parse_struct_subclass(klass, superclass_def)
    elsif klass
      create_attributes(klass, members_from_tags(klass))
    end
    parse_block(:namespace => klass)

    if undocsuper
      raise YARD::Parser::UndocumentableError, 'superclass (class was added without superclass)'
    end
  elsif statement.tokens.to_s =~ /^class\s*<<\s*([\w\:\s]+)/
    classname = $1.gsub(/\s/, '')
    proxy = Proxy.new(namespace, classname)

    # Allow constants to reference class names
    if ConstantObject === proxy
      if proxy.value =~ /\A#{NAMESPACEMATCH}\Z/
        proxy = Proxy.new(namespace, proxy.value)
      else
        raise YARD::Parser::UndocumentableError, "constant class reference '#{classname}'"
      end
    end

    if classname == "self"
      parse_block(:namespace => namespace, :scope => :class)
    elsif classname[0, 1] =~ /[A-Z]/
      register ClassObject.new(namespace, classname) if Proxy === proxy
      parse_block(:namespace => proxy, :scope => :class)
    else
      raise YARD::Parser::UndocumentableError, "class '#{classname}'"
    end
  else
    raise YARD::Parser::UndocumentableError, "class: #{statement.tokens}"
  end
end

#struct_superclass_name(superclass) (private)

[ GitHub ]

  
# File 'lib/yard/handlers/ruby/legacy/class_handler.rb', line 83

def struct_superclass_name(superclass)
  match = superclass.match(/\A(Struct)\.new\((.*?)\)/)
  if match
    paramstring = match[2].split(",")
    first = paramstring.first.strip
    if first[0, 1] =~ /['"]/ && first[-1, 1] =~ /['"]/ && first !~ /\#\{/
      return "Struct::#{first[1..-2]}"
    end
  end
  "Struct"
end