Class: Typelizer::Property

Inherits:
Struct
  • Object
show all
Defined in:
lib/typelizer/property.rb

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



2
3
4
# File 'lib/typelizer/property.rb', line 2

def column_name
  @column_name
end

#column_typeObject

Returns the value of attribute column_type

Returns:

  • (Object)

    the current value of column_type



2
3
4
# File 'lib/typelizer/property.rb', line 2

def column_type
  @column_type
end

#commentObject

Returns the value of attribute comment

Returns:

  • (Object)

    the current value of comment



2
3
4
# File 'lib/typelizer/property.rb', line 2

def comment
  @comment
end

#deprecatedObject

Returns the value of attribute deprecated

Returns:

  • (Object)

    the current value of deprecated



2
3
4
# File 'lib/typelizer/property.rb', line 2

def deprecated
  @deprecated
end

#enumObject

Returns the value of attribute enum

Returns:

  • (Object)

    the current value of enum



2
3
4
# File 'lib/typelizer/property.rb', line 2

def enum
  @enum
end

#enum_type_nameObject

Returns the value of attribute enum_type_name

Returns:

  • (Object)

    the current value of enum_type_name



2
3
4
# File 'lib/typelizer/property.rb', line 2

def enum_type_name
  @enum_type_name
end

#multiObject

Returns the value of attribute multi

Returns:

  • (Object)

    the current value of multi



2
3
4
# File 'lib/typelizer/property.rb', line 2

def multi
  @multi
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/typelizer/property.rb', line 2

def name
  @name
end

#nested_propertiesObject

Returns the value of attribute nested_properties

Returns:

  • (Object)

    the current value of nested_properties



2
3
4
# File 'lib/typelizer/property.rb', line 2

def nested_properties
  @nested_properties
end

#nested_typelizesObject

Returns the value of attribute nested_typelizes

Returns:

  • (Object)

    the current value of nested_typelizes



2
3
4
# File 'lib/typelizer/property.rb', line 2

def nested_typelizes
  @nested_typelizes
end

#nullableObject

Returns the value of attribute nullable

Returns:

  • (Object)

    the current value of nullable



2
3
4
# File 'lib/typelizer/property.rb', line 2

def nullable
  @nullable
end

#optionalObject

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



2
3
4
# File 'lib/typelizer/property.rb', line 2

def optional
  @optional
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



2
3
4
# File 'lib/typelizer/property.rb', line 2

def type
  @type
end

#with_traitsObject

Returns the value of attribute with_traits

Returns:

  • (Object)

    the current value of with_traits



2
3
4
# File 'lib/typelizer/property.rb', line 2

def with_traits
  @with_traits
end

Instance Method Details

#enum_definition(sort_order: :none, prefer_double_quotes: false) ⇒ String?

Generates a TypeScript type definition for named enums

Parameters:

  • sort_order (Symbol, Proc, nil) (defaults to: :none)

    Sort order for enum values (:none, :alphabetical, or Proc)

  • prefer_double_quotes (Boolean) (defaults to: false)

    Whether to use double quotes for string values

Returns:

  • (String, nil)

    The type definition like “type UserRole = ‘admin’ | ‘user’”



69
70
71
72
73
74
75
# File 'lib/typelizer/property.rb', line 69

def enum_definition(sort_order: :none, prefer_double_quotes: false)
  return unless enum && enum_type_name

  values = enum.map { |v| quote_string(v.to_s, prefer_double_quotes) }
  values = values.sort_by(&:downcase) if sort_order == :alphabetical
  "type #{enum_type_name} = #{values.join(" | ")}"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/typelizer/property.rb', line 17

def eql?(other)
  return false unless other.is_a?(self.class)

  fingerprint == other.fingerprint
end

#fingerprintObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/typelizer/property.rb', line 52

def fingerprint
  # Use array format for consistent output across Ruby versions
  # (Hash#inspect format changed in Ruby 3.4)
  # Exclude fields that do not affect generated TypeScript output.
  # Exclude nested_properties/nested_typelizes from to_h to avoid changing
  # fingerprints for properties that don't use them.
  # nested_typelizes is excluded entirely as it only affects inference, not output.
  to_h.except(:column_type, :nested_properties, :nested_typelizes)
    .merge(type: UnionTypeSorter.sort(type_name(sort_order: :alphabetical), :alphabetical))
    .then { |h| nested_properties&.any? ? h.merge(nested_properties: nested_properties.map(&:fingerprint)) : h }
    .to_a.inspect
end

#inspectObject



12
13
14
15
# File 'lib/typelizer/property.rb', line 12

def inspect
  props = to_h.merge(type: type_name).map { |k, v| "#{k}=#{v.inspect}" }.join(" ")
  "<#{self.class.name} #{props}>"
end

#render(sort_order: :none, prefer_double_quotes: false) ⇒ String

Renders the property as a TypeScript property string

Parameters:

  • sort_order (Symbol, Proc, nil) (defaults to: :none)

    Sort order for union types (:none, :alphabetical, or Proc)

  • prefer_double_quotes (Boolean) (defaults to: false)

    Whether to use double quotes for string values

Returns:

  • (String)

    The property string like “name?: Type1 | Type2”



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/typelizer/property.rb', line 32

def render(sort_order: :none, prefer_double_quotes: false)
  type_str = type_name(sort_order: sort_order, prefer_double_quotes: prefer_double_quotes)

  # Handle intersection types for traits
  if with_traits&.any? && type.respond_to?(:name)
    trait_types = with_traits.map { |t| "#{type.name}#{t.to_s.camelize}Trait" }
    type_str = ([type_str] + trait_types).join(" & ")
  end

  type_str = "Array<#{type_str}>" if multi

  # Apply union sorting to the final type string (handles Array<...> unions too)
  type_str = UnionTypeSorter.sort(type_str, sort_order)

  # Add nullable at the end (null should always be last in sorted output)
  type_str = "#{type_str} | null" if nullable

  "#{name}#{"?" if optional}: #{type_str}"
end

#to_sObject

Default to_s for backward compatibility (no sorting)



24
25
26
# File 'lib/typelizer/property.rb', line 24

def to_s
  render(sort_order: :none)
end

#with(**attrs) ⇒ Object



8
9
10
# File 'lib/typelizer/property.rb', line 8

def with(**attrs)
  dup.tap { |p| attrs.each { |k, v| p[k] = v } }
end