Class: Generator::RBS::Unified

Inherits:
Object
  • Object
show all
Includes:
FileWriter, Formatter
Defined in:
lib/generator/rbs/unified.rb

Overview

Unified RBS generator that combines API operations and type definitions in a single file

Constant Summary

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileWriter

#format_files, #write_file

Methods included from Formatter

#convert_doc_links_to_full_url, #convert_html_links_to_yard, #format_method_definition, #split_long_comment_line

Constructor Details

#initialize(api, api_name, api_types) ⇒ Unified

Returns a new instance of Unified.



18
19
20
21
22
# File 'lib/generator/rbs/unified.rb', line 18

def initialize(api, api_name, api_types)
  @api = api
  @api_name = api_name
  @api_types = api_types
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



16
17
18
# File 'lib/generator/rbs/unified.rb', line 16

def api
  @api
end

#api_nameObject (readonly)

Returns the value of attribute api_name.



16
17
18
# File 'lib/generator/rbs/unified.rb', line 16

def api_name
  @api_name
end

#api_typesObject (readonly)

Returns the value of attribute api_types.



16
17
18
# File 'lib/generator/rbs/unified.rb', line 16

def api_types
  @api_types
end

Instance Method Details

#class_nameObject



28
29
30
# File 'lib/generator/rbs/unified.rb', line 28

def class_name
  api.class_name
end

#contentObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generator/rbs/unified.rb', line 36

def content
  lines = []
  lines << "# #{Config::GENERATED_FILE_NOTICE}"
  lines << ""
  lines << "module Peddler"
  lines << "  module APIs"
  lines << "    class #{class_name} < API"

  # Add nested type definitions first (RBS convention)
  sorted_types = api_types.sort_by(&:class_name)
  sorted_types.each do |type|
    class_def = type.rbs_class_definition
    # Indent the class definition with 6 spaces (inside the API class)
    indented_class = indent(class_def, 6)
    lines << indented_class
    lines << "" # Add blank line between classes
  end

  # Add blank line between types and operations if we have both
  lines << "" if api_types.any? && operations.any?

  # Add API operations
  operations.each do |operation|
    operation_line = render_operation(operation)
    lines << "      #{operation_line}" if operation_line
  end

  # Remove trailing blank line if present
  lines.pop if lines.last == ""

  lines << "    end"
  lines << "  end"
  lines << "end"

  lines.join("\n") + "\n"
end

#generateObject



24
25
26
# File 'lib/generator/rbs/unified.rb', line 24

def generate
  write_file(rbs_file_path, content)
end

#operationsObject



32
33
34
# File 'lib/generator/rbs/unified.rb', line 32

def operations
  api.operations
end