module Schematrix::Generators::Templates::SorbetHelpers
Common functions for producing Sorbet output
Constants
- SORBET_TYPE_ANYTHING
-
Sorbet’s spelling of the unconstrained type.
Public Instance Methods
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 32 def array_of(element) = "T::Array[#{element}]"
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 47 def element_union(combined, _terms) = combined
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 25 def enum_type(class_name, members, schema) return enum_type_reference(class_name) if @enums == :classes base_type = members.first.is_a?(String) ? TYPE_STRING : TYPE_INTEGER scalar_type(base_type, schema.format) end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 51 def join_intersection(types) = "T.all(#{types.join(", ")})"
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 49 def join_union(types) = "T.any(#{types.join(", ")})"
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 45 def maybe_type(combined) = "#{maybe_type_name}[#{combined}]"
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 40 def nilable_type(combined) = "T.nilable(#{combined})"
Absence renders through T.nilable; union and intersection terms stay grouped by their constructors
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 36 def null_only_type = "NilClass"
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 18 def scalar_type(type, format = nil) entry = lookup_scalar_type(type, format) return SORBET_TYPE_ANYTHING if entry.nil? entry.sorbet end
The Sorbet spelling of a [type, format] pair, read from the merged scalar type table; an unknown pair renders as unconstrained.
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 53 def sorbet_additional_properties_type @sorbet_additional_properties_type ||= type_of(@path, "additional_properties", @additional_properties) end
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 157 def sorbet_attr_accessor(path, name, property) raise NotImplementedError, "#{self.class} must implement #sorbet_attr_accessor" end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 147 def sorbet_attr_accessors return "" if @properties.nil? @properties .map do |name, property| sorbet_attr_accessor(@path, name, property) end .join("\n\n") end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 61 def sorbet_catch_all_type "T::Hash[String, #{sorbet_additional_properties_type}]" end
The type of the catch-all keyword wherever a signature names it: constructor, copier, and the protected reader all share it.
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 73 def sorbet_coercion_helper_signature "sig { params(value: #{SORBET_TYPE_ANYTHING}, klass: #{ SORBET_TYPE_ANYTHING }).returns(#{SORBET_TYPE_ANYTHING}) }" end
Signature of the shared coercion helpers, which also carry the slot’s target class.
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 65 def sorbet_coercion_signature "sig { params(value: #{SORBET_TYPE_ANYTHING}).returns(#{ SORBET_TYPE_ANYTHING }) }" end
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 100 def sorbet_constructor_signature_params groups = [] unless @properties.nil? || @properties.empty? groups << constructor_properties.map do |name, property| "#{name}: #{property_type_of(@path, name, property)}" end end if immutable? && !@additional_properties.nil? groups << "_extra: #{sorbet_catch_all_type}" end return "" if groups.empty? "params(#{groups.join(", ")})." end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 135 def sorbet_equality_signature "sig { params(other: #{SORBET_TYPE_ANYTHING}).returns(T::Boolean) }" end
The equality signature: the other side stays top, since the body narrows it with the is_a? guard.
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 143 def sorbet_extra_reader_signature "sig { returns(#{sorbet_catch_all_type}) }" end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 87 def sorbet_from_h_signature [ "sig do", " params(hash: T::Hash[String, #{SORBET_TYPE_ANYTHING}])", " .returns(#{own_class_name})", "end", ].join("\n") end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 139 def sorbet_hash_signature "sig { returns(Integer) }" end
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 81 def sorbet_spec_helper_signature "sig { params(value: #{SORBET_TYPE_ANYTHING}, spec: #{ SORBET_TYPE_ANYTHING }).returns(#{SORBET_TYPE_ANYTHING}) }" end
Signature of the spec interpreter, whose second parameter is the conversion spec rather than a slot helper’s class.
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 96 def sorbet_to_h_signature "sig { returns(T::Hash[String, #{SORBET_TYPE_ANYTHING}]) }" end
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 119 def sorbet_with_signature entries = property_names.map do |name| type = property_type_of(@path, name, @properties.fetch(name)) "#{name}: #{type}" end unless @additional_properties.nil? entries << "_extra: #{sorbet_catch_all_type}" end return "sig { returns(T.self_type) }" if entries.empty? "sig { params(#{entries.join(", ")}).returns(T.self_type) }" end
The copier’s signature: one optional keyword per property and one for the catch-all, returning the receiver’s own class so subclasses copy as themselves. A keyword-less copier omits params, which Sorbet refuses empty.
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 34 def string_keyed_hash(value) = "T::Hash[String, #{value}]"
Source
# File lib/schematrix/generators/templates/sorbet_helpers.rb, line 13 def untyped = SORBET_TYPE_ANYTHING