Class: TypeSpecFromSerializers::Interface
- Inherits:
-
Struct
- Object
- Struct
- TypeSpecFromSerializers::Interface
- Defined in:
- lib/typespec_from_serializers/generator.rb
Overview
Internal: Information to generate a TypeSpec model for a serializer.
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#name ⇒ Object
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
Instance Method Summary collapse
- #as_typespec ⇒ Object
- #inspect ⇒ Object
-
#used_imports ⇒ Object
Internal: Returns a list of imports for types used in this model.
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename
113 114 115 |
# File 'lib/typespec_from_serializers/generator.rb', line 113 def filename @filename end |
#name ⇒ Object
Returns the value of attribute name
113 114 115 |
# File 'lib/typespec_from_serializers/generator.rb', line 113 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties
113 114 115 |
# File 'lib/typespec_from_serializers/generator.rb', line 113 def properties @properties end |
Instance Method Details
#as_typespec ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/typespec_from_serializers/generator.rb', line 146 def as_typespec indent = TypeSpecFromSerializers.config.namespace ? 2 : 1 " model \#{name} {\n \#{\" \" * indent}\#{properties.index_by(&:name).values.map(&:as_typespec).join(\"\\n\#{\" \" * indent}\")}\n \#{\" \" * (indent - 1)}}\n TSP\nend\n".gsub(/\n$/, "") |
#inspect ⇒ Object
121 122 123 |
# File 'lib/typespec_from_serializers/generator.rb', line 121 def inspect to_h.inspect end |
#used_imports ⇒ Object
Internal: Returns a list of imports for types used in this model.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/typespec_from_serializers/generator.rb', line 126 def used_imports association_serializers, attribute_types = properties.map(&:type).compact.uniq .partition { |type| type.respond_to?(:tsp_model) } serializer_type_imports = association_serializers.map(&:tsp_model) .map { |type| [type.name, relative_path(type.pathname, pathname)] } custom_type_imports = attribute_types .flat_map { |type| extract_typespec_types(type.to_s) } .uniq .reject { |type| global_type?(type) } .map { |type| type_path = TypeSpecFromSerializers.config.relative_custom_typespec_dir.join(type) [type, relative_path(type_path, pathname)] } (custom_type_imports + serializer_type_imports) .map { |model, filename| %(import "#{filename}.tsp";\n) } end |