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

#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’”



72
73
74
75
76
77
# File 'lib/typelizer/property.rb', line 72

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

  values = sorted_enum_keys(sort_order).map { |k| quote_string(k, prefer_double_quotes) }
  "type #{enum_type_name} = #{values.join(" | ")}"
end

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

Generates a TypeScript runtime constant for named enums

Parameters:

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

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

  • prefer_double_quotes (Boolean) (defaults to: false)

    Whether to use double quotes for string values

Returns:

  • (String, nil)

    The const like “const UserRole = { admin: ‘admin’, user: ‘user’ } as const”



83
84
85
86
87
88
# File 'lib/typelizer/property.rb', line 83

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

  entries = sorted_enum_keys(sort_order).map { |k| "#{js_key(k, prefer_double_quotes)}: #{quote_string(k, prefer_double_quotes)}" }
  "const #{enum_type_name} = { #{entries.join(", ")} } as const"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/typelizer/property.rb', line 21

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

  fingerprint == other.fingerprint
end

#fingerprintObject



59
60
61
62
63
64
65
66
# File 'lib/typelizer/property.rb', line 59

def fingerprint
  # Use array format for consistent output across Ruby versions
  # (Hash#inspect format changed in Ruby 3.4).
  # column_type is excluded because it only informs inference, not output.
  to_h.except(:column_type)
    .merge(type: UnionTypeSorter.sort(type_name(sort_order: :alphabetical), :alphabetical))
    .to_a.inspect
end

#inspectObject



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

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

#lookup_in(hash) ⇒ Object



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

def lookup_in(hash)
  hash[column_name.to_sym] || hash[name.to_sym]
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”



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/typelizer/property.rb', line 42

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

  trait_types = trait_type_names
  type_str = ([type_str] + trait_types).join(" & ") if trait_types.any?

  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)



28
29
30
# File 'lib/typelizer/property.rb', line 28

def to_s
  render(sort_order: :none)
end

#trait_type_namesObject



32
33
34
35
36
# File 'lib/typelizer/property.rb', line 32

def trait_type_names
  return [] unless with_traits&.any? && type.is_a?(Interface)

  with_traits.map { |t| "#{type.name}#{t.to_s.camelize}Trait" }
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