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.
-
#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.
-
#enum_runtime_definition(sort_order: :none, prefer_double_quotes: false) ⇒ String?
Generates a TypeScript runtime constant for named enums.
- #eql?(other) ⇒ Boolean
- #fingerprint ⇒ Object
- #inspect ⇒ Object
- #lookup_in(hash) ⇒ 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).
- #trait_type_names ⇒ Object
- #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 |
#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
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
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
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 |
#fingerprint ⇒ Object
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 |
#inspect ⇒ Object
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
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_s ⇒ Object
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_names ⇒ Object
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 |