Class: YARDSorbet::Handlers::StructPropHandler

Inherits:
YARD::Handlers::Ruby::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/yard-sorbet/handlers/struct_prop_handler.rb

Overview

Handles all const/prop calls, creating accessor methods, and compiles them for later usage at the class level in creating a constructor

Instance Method Summary collapse

Instance Method Details

#decorate_object(object, prop) ⇒ void (private)

This method returns an undefined value.

Add the source and docstring to the method object

Parameters:

  • object (YARD::CodeObjects::MethodObject)
  • prop (TStructProp)


28
29
30
31
32
33
34
35
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 28

def decorate_object(object, prop)
  object.source = prop.source
  # TODO: this should use `+` to delimit the attribute name when markdown is disabled
  reader_docstring = prop.doc.empty? ? "Returns the value of attribute `#{prop.prop_name}`." : prop.doc
  docstring = YARD::DocstringParser.new.parse(reader_docstring).to_docstring
  docstring.add_tag(YARD::Tags::Tag.new(:return, '', prop.types))
  object.docstring = docstring.to_raw
end

#default_valueString? (private)

Get the default prop value

Returns:

  • (String, nil)


39
40
41
42
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 39

def default_value
  default_node = statement.traverse { |n| break n if n.type == :label && n.source == 'default:' }
  default_node.parent[1].source if default_node
end

#make_prop(name) ⇒ TStructProp (private)

Parameters:

  • name (String)

Returns:



45
46
47
48
49
50
51
52
53
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 45

def make_prop(name)
  TStructProp.new(
    default: default_value,
    doc: statement.docstring.to_s,
    prop_name: name,
    source: statement.source,
    types: SigToYARD.convert(statement.parameters[1])
  )
end

#processvoid

This method returns an undefined value.



15
16
17
18
19
20
21
22
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 15

def process
  name = statement.parameters.first.last.last.source
  prop = make_prop(name)
  update_state(prop)
  object = YARD::CodeObjects::MethodObject.new(namespace, name, scope)
  decorate_object(object, prop)
  register_attrs(object, name)
end

#register_attrs(object, name) ⇒ void (private)

This method returns an undefined value.

Register the field explicitly as an attribute. While const attributes are immutable, prop attributes may be reassigned.

Parameters:

  • object (YARD::CodeObjects::MethodObject)
  • name (String)


58
59
60
61
62
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 58

def register_attrs(object, name)
  # Create the virtual method in our current scope
  write = statement.method_name(true) == :prop ? object : nil
  namespace.attributes[scope][name] ||= SymbolHash[read: object, write: write]
end

#update_state(prop) ⇒ void (private)

This method returns an undefined value.

Store the prop for use in the constructor definition

Parameters:



66
67
68
69
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 66

def update_state(prop)
  extra_state.prop_docs ||= Hash.new { |h, k| h[k] = [] }
  extra_state.prop_docs[namespace] << prop
end