Class: TypesFromSerializers::Property

Inherits:
Struct
  • Object
show all
Defined in:
lib/types_from_serializers/generator.rb

Overview

Internal: The type metadata for a serializer attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#column_nameObject

Returns the value of attribute column_name

Returns:

  • (Object)

    the current value of column_name



179
180
181
# File 'lib/types_from_serializers/generator.rb', line 179

def column_name
  @column_name
end

#multiObject

Returns the value of attribute multi

Returns:

  • (Object)

    the current value of multi



179
180
181
# File 'lib/types_from_serializers/generator.rb', line 179

def multi
  @multi
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



179
180
181
# File 'lib/types_from_serializers/generator.rb', line 179

def name
  @name
end

#optionalObject

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



179
180
181
# File 'lib/types_from_serializers/generator.rb', line 179

def optional
  @optional
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



179
180
181
# File 'lib/types_from_serializers/generator.rb', line 179

def type
  @type
end

Instance Method Details

#as_typescriptObject



209
210
211
212
213
214
215
216
217
# File 'lib/types_from_serializers/generator.rb', line 209

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.



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/types_from_serializers/generator.rb', line 195

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

#inspectObject



189
190
191
# File 'lib/types_from_serializers/generator.rb', line 189

def inspect
  to_h.inspect
end