Module: Assistant::RbsGenerator::Renderer

Defined in:
lib/assistant/rbs_generator/renderer.rb

Overview

Converts a Service subclass to a .rbs source string. Pure function; does no I/O.

Class Method Summary collapse

Class Method Details

.render(service_class) ⇒ String

Render a .rbs source string for the given Service subclass. Module-prefix segments of the class name are wrapped in nested module ... end blocks; the trailing segment becomes the class X < Assistant::Service declaration. The body lists one def name: () -> Type and def name?: () -> bool per declared input.

Parameters:

Returns:

  • (String)

    the rendered .rbs source, ending with a newline

Raises:

  • (RuntimeError)

    when service_class is anonymous or declares an input with a non-Class / anonymous type:



18
19
20
21
22
23
24
25
26
27
# File 'lib/assistant/rbs_generator/renderer.rb', line 18

def render(service_class)
  name = service_class.name or raise 'anonymous Service class cannot be rendered'
  segments = name.split('::')
  # `String#split` on a non-empty string always returns at least
  # one element, but Steep can't prove that -- guard for narrowing.
  class_name = segments.pop or raise "unexpected empty name for #{service_class.inspect}"
  body_lines = render_class_body(class_name, service_class.input_definitions)
  nested_lines = nest_in_modules(segments, body_lines)
  "#{[Assistant::RbsGenerator::MARKER, '', *nested_lines].join("\n")}\n"
end