Class: YardStruct::StructHandler
- Inherits:
-
YARD::Handlers::Ruby::Legacy::Base
- Object
- YARD::Handlers::Ruby::Legacy::Base
- YardStruct::StructHandler
- 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
-
#extract_parameters(superstring) ⇒ Array<String>
Extracts the parameter list from the Struct.new declaration and returns it formatted as a list of member names.
-
#normal_class? ⇒ MatchData
Is this a normal class definition (and not a singleton class dereference)?.
-
#process ⇒ Object
Called to process all class definitions.
-
#struct_subclass?(superstring) ⇒ String?
Is this the definition of a Struct/OStruct subclass? If so, return the name of the method.
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
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)?
33 34 35 |
# File 'lib/yard-struct/legacy_handler.rb', line 33 def normal_class? statement.tokens.to_s.match(CLASS_REGEX) end |
#process ⇒ Object
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.
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 |