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



174
175
176
# File 'lib/types_from_serializers/generator.rb', line 174

def column_name
  @column_name
end

#multiObject

Returns the value of attribute multi

Returns:

  • (Object)

    the current value of multi



174
175
176
# File 'lib/types_from_serializers/generator.rb', line 174

def multi
  @multi
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



174
175
176
# File 'lib/types_from_serializers/generator.rb', line 174

def name
  @name
end

#optionalObject

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



174
175
176
# File 'lib/types_from_serializers/generator.rb', line 174

def optional
  @optional
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



174
175
176
# File 'lib/types_from_serializers/generator.rb', line 174

def type
  @type
end

Instance Method Details

#as_typescriptObject



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

#inspectObject



184
185
186
# File 'lib/types_from_serializers/generator.rb', line 184

def inspect
  to_h.inspect
end