Class: Generator::RBS::Types
- Inherits:
-
Object
- Object
- Generator::RBS::Types
- 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)
Instance Method Summary collapse
- #content ⇒ Object
- #generate ⇒ Object
-
#initialize(api_name, api_types) ⇒ Types
constructor
A new instance of Types.
Methods included from FileWriter
Constructor Details
#initialize(api_name, api_types) ⇒ Types
Returns a new instance of Types.
13 14 15 16 |
# File 'lib/generator/rbs/types.rb', line 13 def initialize(api_name, api_types) @api_name = api_name @api_types = api_types end |
Instance Method Details
#content ⇒ Object
22 23 24 25 26 27 28 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 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/generator/rbs/types.rb', line 22 def content lines = [] lines << "# #{Config::GENERATED_FILE_NOTICE}" lines << "" lines << "module Peddler" # Determine if this is a notification, report, feed, or regular type is_notification = @api_name.start_with?("notifications/") is_report = @api_name.start_with?("reports/") is_feed = @api_name.start_with?("feeds/") if is_notification notification_name = @api_name.sub("notifications/", "").camelize lines << " module Notifications" lines << " module #{notification_name}" elsif is_report report_name = @api_name.sub("reports/", "").camelize lines << " module Reports" lines << " module #{report_name}" elsif is_feed feed_name = @api_name.sub("feeds/", "").camelize lines << " module Feeds" lines << " module #{feed_name}" else raise "RBS::Types should only be used for notifications, reports, or feeds. Got: #{@api_name}" end # 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, and feeds, add module-level parse method if is_notification # Find the Notification type to get its fully qualified name notification_type = sorted_types.find { |t| t.class_name == "Notification" } if notification_type full_type_name = "Peddler::Notifications::#{notification_name}::Notification" lines << " def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}" lines << "" end elsif is_report # Find the Report type to get its fully qualified name report_type = sorted_types.find { |t| t.class_name == "Report" } if report_type full_type_name = "Peddler::Reports::#{report_name}::Report" lines << " def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}" lines << "" end elsif is_feed # Find the Feed type to get its fully qualified name feed_type = sorted_types.find { |t| t.class_name == "Feed" } if feed_type full_type_name = "Peddler::Feeds::#{feed_name}::Feed" lines << " def self.parse: (Hash[String | Symbol, untyped]) -> #{full_type_name}" lines << "" end end # Remove trailing blank line lines.pop if lines.last == "" lines << " end" lines << " end" lines << "end" lines.join("\n") + "\n" end |
#generate ⇒ Object
18 19 20 |
# File 'lib/generator/rbs/types.rb', line 18 def generate write_file(rbs_file_path, content) end |