Class: Typelizer::Interface
- Inherits:
-
Object
- Object
- Typelizer::Interface
- Includes:
- TypeInference
- Defined in:
- lib/typelizer/interface.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
- #config ⇒ Object
- #empty? ⇒ Boolean
- #enum_types ⇒ Object
- #filename ⇒ Object
- #fingerprint ⇒ Object
- #imports ⇒ Object
- #index_path(index_dir) ⇒ Object
-
#initialize(serializer:, context:) ⇒ Interface
constructor
A new instance of Interface.
- #inline? ⇒ Boolean
- #inspect ⇒ Object
- #meta_fields ⇒ Object
- #name ⇒ Object
- #overwritten_properties ⇒ Object
- #own_properties ⇒ Object
- #parent_interface ⇒ Object
- #properties ⇒ Object
- #properties_to_print ⇒ Object
- #quote(str) ⇒ Object
- #root_key ⇒ Object
- #serializer_plugin ⇒ Object
- #trait_interfaces ⇒ Object
Constructor Details
#initialize(serializer:, context:) ⇒ Interface
Returns a new instance of Interface.
9 10 11 12 |
# File 'lib/typelizer/interface.rb', line 9 def initialize(serializer:, context:) @serializer = serializer @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/typelizer/interface.rb', line 7 def context @context end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
7 8 9 |
# File 'lib/typelizer/interface.rb', line 7 def serializer @serializer end |
Instance Method Details
#config ⇒ Object
14 15 16 |
# File 'lib/typelizer/interface.rb', line 14 def config context.config_for(serializer) end |
#empty? ⇒ Boolean
59 60 61 |
# File 'lib/typelizer/interface.rb', line 59 def empty? .empty? && properties.empty? end |
#enum_types ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/typelizer/interface.rb', line 78 def enum_types @enum_types ||= begin all_properties = collect_all_properties(properties + trait_interfaces.flat_map(&:properties)) all_properties .select(&:enum_definition) .uniq(&:enum_type_name) end end |
#filename ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/typelizer/interface.rb', line 38 def filename if config.filename_mapper config.filename_mapper.call(config.serializer_name_mapper.call(serializer)) else name.gsub("::", "/") end end |
#fingerprint ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/typelizer/interface.rb', line 160 def fingerprint [ name, properties_to_print.map(&:fingerprint), parent_interface&.name, root_key, .map(&:fingerprint), trait_interfaces.map { |t| [t.name, t.properties.map(&:fingerprint)] }, CONFIGS_AFFECTING_OUTPUT.map { |key| config.public_send(key) } ].inspect end |
#imports ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/typelizer/interface.rb', line 122 def imports @imports ||= begin # Include both main properties and trait properties for import collection, # recursively including nested sub-properties all_properties = collect_all_properties(properties_to_print + trait_interfaces.flat_map(&:properties)) flat_types = all_properties.filter_map(&:type).flat_map { |t| Array(t) }.uniq association_serializers, attribute_types = flat_types.partition { |type| type.is_a?(Interface) } serializer_types = association_serializers .filter_map { |interface| interface.name if interface.name != name && !interface.inline? } custom_type_imports = attribute_types .flat_map { |type| extract_typescript_types(type.to_s) } .uniq .reject { |type| global_type?(type) } # Collect trait types from properties with with_traits (skip self-references) trait_imports = all_properties.flat_map do |prop| next [] unless prop.with_traits&.any? && prop.type.is_a?(Interface) # Skip if the trait types are from the current interface (same file) next [] if prop.type.name == name prop.with_traits.map { |t| "#{prop.type.name}#{t.to_s.camelize}Trait" } end # Collect enum type names from properties enum_imports = all_properties.filter_map(&:enum_type_name) result = (custom_type_imports + serializer_types + trait_imports + enum_imports + Array(parent_interface&.name)).uniq - [self_type_name, name] ImportSorter.sort(result, config.imports_sort_order) end end |
#index_path(index_dir) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/typelizer/interface.rb', line 46 def index_path(index_dir) iface_dir = config.output_dir.to_s if iface_dir != index_dir Pathname.new(File.join(iface_dir, filename)).relative_path_from(Pathname.new(index_dir)).to_s else "./#{filename}" end end |
#inline? ⇒ Boolean
26 27 28 |
# File 'lib/typelizer/interface.rb', line 26 def inline? !serializer.is_a?(Class) || serializer.name.nil? end |
#inspect ⇒ Object
156 157 158 |
# File 'lib/typelizer/interface.rb', line 156 def inspect "<#{self.class.name} #{name} properties=#{properties.inspect}>" end |
#meta_fields ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/typelizer/interface.rb', line 63 def @meta_fields ||= begin props = serializer_plugin. || [] props = infer_types(props, :_typelizer_meta_attributes) props = config.properties_transformer.call(props) if config.properties_transformer PropertySorter.sort(props, config.properties_sort_order) end end |
#name ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/typelizer/interface.rb', line 30 def name if inline? Renderer.call("inline_type.ts.erb", properties: properties, sort_order: config.properties_sort_order).strip else config.serializer_name_mapper.call(serializer).tr_s(":", "") end end |
#overwritten_properties ⇒ Object
96 97 98 99 100 |
# File 'lib/typelizer/interface.rb', line 96 def overwritten_properties return [] unless parent_interface @overwritten_properties ||= parent_interface.properties - properties end |
#own_properties ⇒ Object
102 103 104 |
# File 'lib/typelizer/interface.rb', line 102 def own_properties @own_properties ||= properties - (parent_interface&.properties || []) end |
#parent_interface ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/typelizer/interface.rb', line 110 def parent_interface return if config.inheritance_strategy == :none parent_class = serializer.superclass return unless parent_class.respond_to?(:typelizer_config) parent_interface = context.interface_for(parent_class) return if parent_interface.empty? parent_interface end |
#properties ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/typelizer/interface.rb', line 87 def properties @properties ||= begin props = serializer_plugin.properties props = infer_types(props) props = config.properties_transformer.call(props) if config.properties_transformer PropertySorter.sort(props, config.properties_sort_order) end end |
#properties_to_print ⇒ Object
106 107 108 |
# File 'lib/typelizer/interface.rb', line 106 def properties_to_print parent_interface ? own_properties : properties end |
#quote(str) ⇒ Object
172 173 174 |
# File 'lib/typelizer/interface.rb', line 172 def quote(str) config.prefer_double_quotes ? "\"#{str}\"" : "'#{str}'" end |
#root_key ⇒ Object
55 56 57 |
# File 'lib/typelizer/interface.rb', line 55 def root_key serializer_plugin.root_key end |
#serializer_plugin ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/typelizer/interface.rb', line 18 def serializer_plugin @serializer_plugin ||= config.serializer_plugin.new( serializer: serializer, config: config, context: context ) end |
#trait_interfaces ⇒ Object
72 73 74 75 76 |
# File 'lib/typelizer/interface.rb', line 72 def trait_interfaces return [] unless serializer_plugin.respond_to?(:trait_interfaces) @trait_interfaces ||= serializer_plugin.trait_interfaces end |