Module: T::Utils
- Defined in:
- lib/types/utils.rb
Overview
typed: true
Defined Under Namespace
Class Method Summary collapse
-
.arity(method) ⇒ Object
Returns the arity of a method, unwrapping the sig if needed.
-
.check_type_recursive!(value, type) ⇒ Object
Dynamically confirm that `value` is recursively a valid value of type `type`, including recursively through collections.
-
.coerce(val) ⇒ Object
Used to convert from a type specification to a `T::Types::Base`.
- .lift_enum(enum) ⇒ Object
-
.methods_excluding_object(mod) ⇒ Object
Returns the set of all methods (public, protected, private) defined on a module or its ancestors, excluding Object and its ancestors.
-
.resolve_alias(type) ⇒ Object
Return the underlying type for a type alias.
-
.run_all_sig_blocks ⇒ Object
Unwraps all the sigs.
-
.signature_for_instance_method(mod, method_name) ⇒ Object
Returns the signature for the instance method on the supplied module, or nil if it's not found or not typed.
-
.signature_for_method(method) ⇒ Object
Returns the signature for the `UnboundMethod`, or nil if it's not sig'd.
-
.string_truncate_middle(str, start_len, end_len, ellipsis = '...') ⇒ String
Elide the middle of a string as needed and replace it with an ellipsis.
-
.unwrap_nilable(type) ⇒ Object
Give a type which is a subclass of T::Types::Base, determines if the type is a simple nilable type (union of NilClass and something else).
- .wrap_method_with_call_validation_if_needed(mod, method_sig, original_method) ⇒ Object
Class Method Details
.arity(method) ⇒ Object
Returns the arity of a method, unwrapping the sig if needed
115 116 117 118 119 120 |
# File 'lib/types/utils.rb', line 115 def self.arity(method) arity = method.arity return arity if arity != -1 || method.is_a?(Proc) sig = T::Private::Methods.signature_for_method(method) sig ? sig.method.arity : arity end |
.check_type_recursive!(value, type) ⇒ Object
Dynamically confirm that `value` is recursively a valid value of type `type`, including recursively through collections. Note that in some cases this runtime check can be very expensive, especially with large collections of objects.
46 47 48 |
# File 'lib/types/utils.rb', line 46 def self.check_type_recursive!(value, type) T::Private::Casts.cast_recursive(value, type, "T.check_type_recursive!") end |
.coerce(val) ⇒ Object
Used to convert from a type specification to a `T::Types::Base`.
38 39 40 |
# File 'lib/types/utils.rb', line 38 def self.coerce(val) Private.coerce_and_check_module_types(val, nil, false) end |
.lift_enum(enum) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/types/utils.rb', line 158 def self.lift_enum(enum) unless enum.is_a?(T::Types::Enum) raise ArgumentError.new("#{enum.inspect} is not a T.deprecated_enum") end classes = enum.values.map(&:class).uniq if classes.empty? T.untyped elsif classes.length > 1 T::Types::Union.new(classes) else T::Types::Simple::Private::Pool.type_for_module(classes.first) end end |
.methods_excluding_object(mod) ⇒ Object
Returns the set of all methods (public, protected, private) defined on a module or its ancestors, excluding Object and its ancestors. Overrides of methods from Object (and its ancestors) are included.
53 54 55 56 57 58 59 60 61 |
# File 'lib/types/utils.rb', line 53 def self.methods_excluding_object(mod) # We can't just do mod.instance_methods - Object.instance_methods, because that would leave out # any methods from Object that are overridden in mod. mod.ancestors.flat_map do |ancestor| # equivalent to checking Object.ancestors.include?(ancestor) next [] if Object <= ancestor ancestor.instance_methods(false) + ancestor.private_instance_methods(false) end.uniq end |
.resolve_alias(type) ⇒ Object
Return the underlying type for a type alias. Otherwise returns type.
87 88 89 90 91 92 93 94 |
# File 'lib/types/utils.rb', line 87 def self.resolve_alias(type) case type when T::Private::Types::TypeAlias type.aliased_type else type end end |
.run_all_sig_blocks ⇒ Object
Unwraps all the sigs.
82 83 84 |
# File 'lib/types/utils.rb', line 82 def self.run_all_sig_blocks T::Private::Methods.run_all_sig_blocks end |
.signature_for_instance_method(mod, method_name) ⇒ Object
Returns the signature for the instance method on the supplied module, or nil if it's not found or not typed.
73 74 75 |
# File 'lib/types/utils.rb', line 73 def self.signature_for_instance_method(mod, method_name) T::Private::Methods.signature_for_method(mod.instance_method(method_name)) end |
.signature_for_method(method) ⇒ Object
Returns the signature for the `UnboundMethod`, or nil if it's not sig'd
66 67 68 |
# File 'lib/types/utils.rb', line 66 def self.signature_for_method(method) T::Private::Methods.signature_for_method(method) end |
.string_truncate_middle(str, start_len, end_len, ellipsis = '...') ⇒ String
Elide the middle of a string as needed and replace it with an ellipsis. Keep the given number of characters at the start and end of the string.
This method operates on string length, not byte length.
If the string is shorter than the requested truncation length, return it without adding an ellipsis. This method may return a longer string than the original if the characters removed are shorter than the ellipsis.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/types/utils.rb', line 140 def self.string_truncate_middle(str, start_len, end_len, ellipsis='...') return unless str raise ArgumentError.new('must provide start_len') unless start_len raise ArgumentError.new('must provide end_len') unless end_len raise ArgumentError.new('start_len must be >= 0') if start_len < 0 raise ArgumentError.new('end_len must be >= 0') if end_len < 0 str = str.to_s return str if str.length <= start_len + end_len start_part = str[0...start_len - ellipsis.length] end_part = end_len == 0 ? '' : str[-end_len..-1] "#{start_part}#{ellipsis}#{end_part}" end |
.unwrap_nilable(type) ⇒ Object
Give a type which is a subclass of T::Types::Base, determines if the type is a simple nilable type (union of NilClass and something else). If so, returns the T::Types::Base of the something else. Otherwise, returns nil.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/types/utils.rb', line 98 def self.unwrap_nilable(type) case type when T::Types::Union non_nil_types = type.types.reject {|t| t == Nilable::NIL_TYPE} return nil if type.types.length == non_nil_types.length case non_nil_types.length when 0 then nil when 1 then non_nil_types.first else T::Types::Union::Private::Pool.union_of_types(non_nil_types[0], non_nil_types[1], non_nil_types[2..-1]) end else nil end end |
.wrap_method_with_call_validation_if_needed(mod, method_sig, original_method) ⇒ Object
77 78 79 |
# File 'lib/types/utils.rb', line 77 def self.wrap_method_with_call_validation_if_needed(mod, method_sig, original_method) T::Private::Methods::CallValidation.wrap_method_if_needed(mod, method_sig, original_method) end |