Class: TypesFromSerializers::Property
- Inherits:
-
Struct
- Object
- Struct
- TypesFromSerializers::Property
- Defined in:
- lib/types_from_serializers/generator.rb
Overview
Internal: The type metadata for a serializer attribute.
Instance Attribute Summary collapse
-
#column_name ⇒ Object
Returns the value of attribute column_name.
-
#multi ⇒ Object
Returns the value of attribute multi.
-
#name ⇒ Object
Returns the value of attribute name.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #as_typescript ⇒ Object
-
#infer_type_from(columns_hash, defined_enums, ts_interface) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeScript interface if provided.
- #inspect ⇒ Object
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name
174 175 176 |
# File 'lib/types_from_serializers/generator.rb', line 174 def column_name @column_name end |
#multi ⇒ Object
Returns the value of attribute multi
174 175 176 |
# File 'lib/types_from_serializers/generator.rb', line 174 def multi @multi end |
#name ⇒ Object
Returns the value of attribute name
174 175 176 |
# File 'lib/types_from_serializers/generator.rb', line 174 def name @name end |
#optional ⇒ Object
Returns the value of attribute optional
174 175 176 |
# File 'lib/types_from_serializers/generator.rb', line 174 def optional @optional end |
#type ⇒ Object
Returns the value of attribute type
174 175 176 |
# File 'lib/types_from_serializers/generator.rb', line 174 def type @type end |
Instance Method Details
#as_typescript ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/types_from_serializers/generator.rb', line 204 def as_typescript type_str = if type.respond_to?(:ts_name) type.ts_name else type || TypesFromSerializers.config.unknown_type end "#{name}#{"?" if optional}: #{type_str}#{"[]" if multi}" end |
#infer_type_from(columns_hash, defined_enums, ts_interface) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeScript interface if provided.
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/types_from_serializers/generator.rb', line 190 def infer_type_from(columns_hash, defined_enums, ts_interface) if type type elsif (enum = defined_enums[column_name.to_s]) self.type = enum.keys.map(&:inspect).join(" | ") elsif (column = columns_hash[column_name.to_s]) self.multi = true if column.try(:array) self.optional = true if column.null && !column.default self.type = TypesFromSerializers.config.sql_to_typescript_type_mapping[column.type] elsif ts_interface self.type = "#{ts_interface}['#{name}']" end end |
#inspect ⇒ Object
184 185 186 |
# File 'lib/types_from_serializers/generator.rb', line 184 def inspect to_h.inspect end |