Class: YardStruct::StructHandler

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

Constant Summary collapse

CLASS_REGEX =
/^class\s+(#{NAMESPACEMATCH})\s*(?:<\s*(.+)|\Z)/m

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(superstring) ⇒ Array<String>

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:

  • (Array<String>)

    a list of member names



55
56
57
58
# File 'lib/yard-struct/legacy_handler.rb', line 55

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

#normal_class?MatchData

Is this a normal class definition (and not a singleton class dereference)?

Returns:

  • (MatchData)

    a match that will contain the class name in the first entry and the superclass in the second entry as strings.



33
34
35
# File 'lib/yard-struct/legacy_handler.rb', line 33

def normal_class?
  statement.tokens.to_s.match(CLASS_REGEX)
end

#processObject

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yard-struct/legacy_handler.rb', line 11

def process
  # matches normal classes
  if match = normal_class?
    classname, klass_string = match[1], match[2]
    # is it a struct/ostruct subclass
    if superclass = struct_subclass?(klass_string)
      # get the class
      klass = create_class(classname, superclass)

      # Get the members
      params = extract_parameters klass_string
      
      create_attributes(klass, params)
    end # end if struct subclass
  end # end if normal class declaration
end

#struct_subclass?(superstring) ⇒ String?

Is this the definition of a Struct/OStruct subclass? If so, return the name of the method.

Parameters:

  • superstring (String)

    the string saying what the superclass is

Returns:

  • (String, nil)

    the name of the superclass type, or nil if it’s not a Struct or OStruct



44
45
46
# File 'lib/yard-struct/legacy_handler.rb', line 44

def struct_subclass?(superstring)
  superstring && (superstring.match(/\A(O?Struct)\.new\((.*?)\)/) ? $1 : nil)
end