Class: Typelizer::Property
- Inherits:
-
Struct
- Object
- Struct
- Typelizer::Property
- Defined in:
- lib/typelizer/property.rb
Instance Attribute Summary collapse
-
#column_name ⇒ Object
Returns the value of attribute column_name.
-
#column_type ⇒ Object
Returns the value of attribute column_type.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#deprecated ⇒ Object
Returns the value of attribute deprecated.
-
#enum ⇒ Object
Returns the value of attribute enum.
-
#enum_type_name ⇒ Object
Returns the value of attribute enum_type_name.
-
#multi ⇒ Object
Returns the value of attribute multi.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nested_properties ⇒ Object
Returns the value of attribute nested_properties.
-
#nested_typelizes ⇒ Object
Returns the value of attribute nested_typelizes.
-
#nullable ⇒ Object
Returns the value of attribute nullable.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#type ⇒ Object
Returns the value of attribute type.
-
#with_traits ⇒ Object
Returns the value of attribute with_traits.
Instance Method Summary collapse
-
#enum_definition(sort_order: :none, prefer_double_quotes: false) ⇒ String?
Generates a TypeScript type definition for named enums.
- #eql?(other) ⇒ Boolean
- #fingerprint ⇒ Object
- #inspect ⇒ Object
-
#render(sort_order: :none, prefer_double_quotes: false) ⇒ String
Renders the property as a TypeScript property string.
-
#to_s ⇒ Object
Default to_s for backward compatibility (no sorting).
- #with(**attrs) ⇒ Object
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def column_name @column_name end |
#column_type ⇒ Object
Returns the value of attribute column_type
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def column_type @column_type end |
#comment ⇒ Object
Returns the value of attribute comment
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def comment @comment end |
#deprecated ⇒ Object
Returns the value of attribute deprecated
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def deprecated @deprecated end |
#enum ⇒ Object
Returns the value of attribute enum
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def enum @enum end |
#enum_type_name ⇒ Object
Returns the value of attribute enum_type_name
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def enum_type_name @enum_type_name end |
#multi ⇒ Object
Returns the value of attribute multi
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def multi @multi end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def name @name end |
#nested_properties ⇒ Object
Returns the value of attribute nested_properties
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def nested_properties @nested_properties end |
#nested_typelizes ⇒ Object
Returns the value of attribute nested_typelizes
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def nested_typelizes @nested_typelizes end |
#nullable ⇒ Object
Returns the value of attribute nullable
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def nullable @nullable end |
#optional ⇒ Object
Returns the value of attribute optional
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def optional @optional end |
#type ⇒ Object
Returns the value of attribute type
2 3 4 |
# File 'lib/typelizer/property.rb', line 2 def type @type end |
#with_traits ⇒ Object
Returns the value of attribute 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
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
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 |
#fingerprint ⇒ Object
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 |
#inspect ⇒ Object
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
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_s ⇒ Object
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 |