Class: TypeSpecFromSerializers::Property
- Inherits:
-
Struct
- Object
- Struct
- TypeSpecFromSerializers::Property
- Defined in:
- lib/typespec_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_typespec ⇒ Object
-
#infer_typespec_from(columns_hash, defined_enums, tsp_model) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeSpec model if provided.
- #inspect ⇒ Object
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name
180 181 182 |
# File 'lib/typespec_from_serializers/generator.rb', line 180 def column_name @column_name end |
#multi ⇒ Object
Returns the value of attribute multi
180 181 182 |
# File 'lib/typespec_from_serializers/generator.rb', line 180 def multi @multi end |
#name ⇒ Object
Returns the value of attribute name
180 181 182 |
# File 'lib/typespec_from_serializers/generator.rb', line 180 def name @name end |
#optional ⇒ Object
Returns the value of attribute optional
180 181 182 |
# File 'lib/typespec_from_serializers/generator.rb', line 180 def optional @optional end |
#type ⇒ Object
Returns the value of attribute type
180 181 182 |
# File 'lib/typespec_from_serializers/generator.rb', line 180 def type @type end |
Instance Method Details
#as_typespec ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/typespec_from_serializers/generator.rb', line 210 def as_typespec type_str = if type.respond_to?(:tsp_name) type.tsp_name else type || TypeSpecFromSerializers.config.unknown_type end "#{name}#{"?" if optional}: #{type_str}#{"[]" if multi};" end |
#infer_typespec_from(columns_hash, defined_enums, tsp_model) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeSpec model if provided.
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/typespec_from_serializers/generator.rb', line 196 def infer_typespec_from(columns_hash, defined_enums, tsp_model) 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 = TypeSpecFromSerializers.config.sql_to_typespec_type_mapping[column.type] elsif tsp_model self.type = "#{tsp_model}.#{name}::type" end end |
#inspect ⇒ Object
190 191 192 |
# File 'lib/typespec_from_serializers/generator.rb', line 190 def inspect to_h.inspect end |