Class: Generator::RBS::Types

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

Overview

Generates consolidated RBS signature files for types in notifications, reports, and feeds (These don't have API operations, just type definitions)

Constant Summary collapse

KIND_DEFS =
[
  ["notifications/", "Notifications", "sig/peddler/notifications", :notification],
  ["reports/", "Reports", "sig/peddler/reports", :report],
  ["feeds/", "Feeds", "sig/peddler/feeds", :feed],
  ["data_kiosk/", "DataKiosk", "sig/peddler/data_kiosk", :data_kiosk],
].freeze

Instance Method Summary collapse

Methods included from FileWriter

#format_files, #write_file

Constructor Details

#initialize(api_name, api_types) ⇒ Types

Returns a new instance of Types.



20
21
22
23
# File 'lib/generator/rbs/types.rb', line 20

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

Instance Method Details

#contentObject



29
30
31
32
33
34
35
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generator/rbs/types.rb', line 29

def content
  context = api_context
  lines = []
  lines << "# #{Config::GENERATED_FILE_NOTICE}"
  lines << ""
  lines << "module Peddler"
  lines << "  module #{context[:module_name]}"
  lines << "    module #{context[:name]}"

  # Sort types alphabetically by class name for consistent output
  sorted_types = @api_types.sort_by(&:class_name)

  # Generate each type's class definition
  sorted_types.each do |type|
    class_def = type.rbs_class_definition
    # Indent the class definition with 6 spaces (3 levels of nesting)
    indented_class = indent(class_def, 6)
    lines << indented_class
    lines << "" # Add blank line between classes
  end

  # For notifications, reports, feeds, and data_kiosk, add module-level parse method
  case context[:kind]
  when :notification
    notification_type = sorted_types.find { |t| t.class_name == "Notification" }
    if notification_type
      full_type_name = "Peddler::#{context[:module_name]}::#{context[:name]}::Notification"
      lines << "      def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}"
      lines << ""
    end
  when :report
    report_type = sorted_types.find { |t| t.class_name == "Report" }
    if report_type
      full_type_name = "Peddler::#{context[:module_name]}::#{context[:name]}::Report"
      lines << "      def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}"
      lines << ""
    end
  when :feed
    feed_type = sorted_types.find { |t| t.class_name == "Feed" }
    if feed_type
      full_type_name = "Peddler::#{context[:module_name]}::#{context[:name]}::Feed"
      lines << "      def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}"
      lines << ""
    end
  when :data_kiosk
    lines << "      def self.schema: () -> Hash[String, untyped]"
    lines << ""
  end

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

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

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

#generateObject



25
26
27
# File 'lib/generator/rbs/types.rb', line 25

def generate
  write_file(rbs_file_path, content)
end