Module: T
- Defined in:
- lib/crystalline/t.rb
Overview
typed: true frozen_string_literal: true
Class Method Summary collapse
- .arr?(t) ⇒ Boolean
- .arr_of(t) ⇒ Object
- .get_raw_type(t) ⇒ Object
- .get_union_types(t) ⇒ Object
- .hash?(t) ⇒ Boolean
- .hash_of(t) ⇒ Object
- .nilable?(t) ⇒ Boolean
- .nilable_of(t) ⇒ Object
- .simplifiable?(t) ⇒ Boolean
- .simplify_type(t) ⇒ Object
- .union?(t) ⇒ Boolean
Class Method Details
.arr?(t) ⇒ Boolean
7 8 9 10 11 12 |
# File 'lib/crystalline/t.rb', line 7 def self.arr?(t) if t.respond_to? :underlying_class return t. == ::Array end false end |
.arr_of(t) ⇒ Object
14 15 16 |
# File 'lib/crystalline/t.rb', line 14 def self.arr_of(t) get_raw_type(t.type) end |
.get_raw_type(t) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/crystalline/t.rb', line 70 def self.get_raw_type(t) if t.respond_to? :raw_type return t.raw_type elsif t.respond_to? :raw_a return T::Boolean if t.raw_a in [TrueClass, FalseClass] end t end |
.get_union_types(t) ⇒ Object
65 66 67 |
# File 'lib/crystalline/t.rb', line 65 def self.get_union_types(t) t.types.map { |tt| get_raw_type(tt) } end |
.hash?(t) ⇒ Boolean
18 19 20 21 22 23 |
# File 'lib/crystalline/t.rb', line 18 def self.hash?(t) if t.respond_to? :underlying_class return t. == ::Hash end false end |
.hash_of(t) ⇒ Object
25 26 27 |
# File 'lib/crystalline/t.rb', line 25 def self.hash_of(t) get_raw_type(t.values) end |
.nilable?(t) ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/crystalline/t.rb', line 29 def self.nilable?(t) # in sorbet all `T.any` types provide unwrap_nilable for some reason # So, in order to make this check more robust, we check if the type responds to the `types` method, and if so, whether # one of those types is NilClass. For non-nilable unions, `types` will return a valid list that does not include NilClass return false unless t.respond_to?(:unwrap_nilable) return true unless t.respond_to?(:types) t.types.map { |tt| simplify_type(tt) }.include?(NilClass) end |
.nilable_of(t) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/crystalline/t.rb', line 38 def self.nilable_of(t) if nilable t return simplify_type t.unwrap_nilable end nil end |
.simplifiable?(t) ⇒ Boolean
46 47 48 |
# File 'lib/crystalline/t.rb', line 46 def self.simplifiable?(t) return t.is_a? T::Types::Simple end |
.simplify_type(t) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/crystalline/t.rb', line 50 def self.simplify_type(t) if t.is_a? T::Types::Simple return t.raw_type end t end |
.union?(t) ⇒ Boolean
58 59 60 61 62 63 |
# File 'lib/crystalline/t.rb', line 58 def self.union?(t) return false unless t.respond_to? :types return false if t.types.any? { |tt| get_raw_type(tt) == TrueClass } return false if t.types.length == 2 && t.types.any? { |tt| get_raw_type(tt) == NilClass } t.types.length > 1 end |