Class: YARDSorbet::Handlers::StructPropHandler
- Inherits:
-
YARD::Handlers::Ruby::Base
- Object
- YARD::Handlers::Ruby::Base
- YARDSorbet::Handlers::StructPropHandler
- 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
-
#decorate_object(object, prop) ⇒ void
private
Add the source and docstring to the method object.
- #immutable? ⇒ Boolean private
-
#kw_arg(kwd) ⇒ String?
private
The value passed to the keyword argument, or nil.
- #make_prop(name) ⇒ TStructProp private
- #params ⇒ Array<YARD::Parser::Ruby::AstNode> private
- #process ⇒ void
-
#register_attrs(object, name) ⇒ void
private
Register the field explicitly as an attribute.
-
#update_state(prop) ⇒ void
private
Store the prop for use in the constructor definition.
Instance Method Details
#decorate_object(object, prop) ⇒ void (private)
This method returns an undefined value.
Add the source and docstring to the method object
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 prop name when markdown is disabled reader_docstring = prop.doc.empty? ? "Returns the value of prop `#{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 |
#immutable? ⇒ Boolean (private)
38 |
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 38 def immutable? = statement.method_name(true) == :const || kw_arg('immutable:') == 'true' |
#kw_arg(kwd) ⇒ String? (private)
Returns the value passed to the keyword argument, or nil.
42 |
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 42 def kw_arg(kwd) = params[2]&.find { _1[0].source == kwd }&.[](1)&.source |
#make_prop(name) ⇒ TStructProp (private)
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: kw_arg('default:'), doc: statement.docstring.to_s, prop_name: name, source: statement.source, types: SigToYARD.convert(params.fetch(1)) ) end |
#params ⇒ Array<YARD::Parser::Ruby::AstNode> (private)
56 |
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 56 def params = @params ||= T.let(statement.parameters(false), T.nilable(T::Array[YARD::Parser::Ruby::AstNode])) |
#process ⇒ void
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 = params.dig(0, -1, -1).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.
60 61 62 63 64 |
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 60 def register_attrs(object, name) write = immutable? ? nil : object # Create the virtual attribute in our current scope namespace.attributes[scope][name] ||= SymbolHash[read: object, write:] end |
#update_state(prop) ⇒ void (private)
This method returns an undefined value.
Store the prop for use in the constructor definition
68 69 70 71 |
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 68 def update_state(prop) extra_state.prop_docs ||= Hash.new { |h, k| h[k] = [] } extra_state.prop_docs[namespace] << prop end |