Class: YardStruct::ModernStructHandler

Inherits:
YARD::Handlers::Ruby::Base
  • Object
show all
Includes:
SharedMethods
Defined in:
lib/yard-struct/modern_handler.rb

Overview

This handler is used by the Ruby 1.9+ parser engine. Uses AST nodes.

All the interesting logic is actually all in SharedMethods. This class specifically defines the parsing logic to get the data ready for the Shared Methods.

Instance Method Summary collapse

Methods included from SharedMethods

#create_attributes, #create_class, #create_reader, #create_writer, #getter_docstring, #member_tag_for_member, #return_type_for_member, #setter_docstring

Instance Method Details

#extract_parameters(superclass) ⇒ Array<String>

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



37
38
39
40
# File 'lib/yard-struct/modern_handler.rb', line 37

def extract_parameters(superclass)
  members = superclass.parameters.dup[0..-2] # drop the "false" at the end
  members.map {|x| x.source.strip[1..-1]}
end

#parse_superclass(superclass) ⇒ String?

Extracts the superclass name from the class definition, returning ‘nil` if we get something other than a Struct/OStruct subclass.



49
50
51
52
53
54
55
56
57
58
# File 'lib/yard-struct/modern_handler.rb', line 49

def parse_superclass(superclass)
  return nil unless superclass
  return nil unless superclass.type == :call || superclass.type == :command_call
  
  cname = superclass.namespace.source
  if cname =~ /^O?Struct$/ && superclass.method_name(true) == :new
    return cname
  end
  nil
end

#processObject

Called to process all class definitions. We’ll ignore anything but subclasses of Struct.new()



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yard-struct/modern_handler.rb', line 17

def process
  classname = statement[0].source
  superclass = parse_superclass(statement[1])
  # did we get a superclass worth parsing?
  if superclass
    # get the class
    klass = create_class(classname, superclass)
    # get the members
    members = extract_parameters(statement[1])
    # create all the members
    create_attributes(klass, members)
  end
end