Class: TypesFromSerializers::Interface
- Inherits:
-
Struct
- Object
- Struct
- TypesFromSerializers::Interface
- Defined in:
- lib/types_from_serializers/generator.rb
Overview
Internal: Information to generate a TypeScript interface 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_typescript ⇒ Object
- #inspect ⇒ Object
-
#used_imports ⇒ Object
Internal: Returns a list of imports for types used in this interface.
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename
107 108 109 |
# File 'lib/types_from_serializers/generator.rb', line 107 def filename @filename end |
#name ⇒ Object
Returns the value of attribute name
107 108 109 |
# File 'lib/types_from_serializers/generator.rb', line 107 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties
107 108 109 |
# File 'lib/types_from_serializers/generator.rb', line 107 def properties @properties end |
Instance Method Details
#as_typescript ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/types_from_serializers/generator.rb', line 140 def as_typescript indent = TypesFromSerializers.config.namespace ? 3 : 1 <<~TS.gsub(/\n$/, "") interface #{name} { #{" " * indent}#{properties.index_by(&:name).values.map(&:as_typescript).join("\n#{" " * indent}")} #{" " * (indent - 1)}} TS end |
#inspect ⇒ Object
115 116 117 |
# File 'lib/types_from_serializers/generator.rb', line 115 def inspect to_h.inspect end |
#used_imports ⇒ Object
Internal: Returns a list of imports for types used in this interface.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/types_from_serializers/generator.rb', line 120 def used_imports association_serializers, attribute_types = properties.map(&:type).compact.uniq .partition { |type| type.respond_to?(:ts_interface) } serializer_type_imports = association_serializers.map(&:ts_interface) .map { |type| [type.name, relative_path(type.pathname, pathname)] } custom_type_imports = attribute_types .flat_map { |type| extract_typescript_types(type.to_s) } .uniq .reject { |type| global_type?(type) } .map { |type| type_path = TypesFromSerializers.config.relative_custom_types_dir.join(type) [type, relative_path(type_path, pathname)] } (custom_type_imports + serializer_type_imports) .map { |interface, filename| "import type #{interface} from '#{filename}'\n" } end |