Class: T::Types::Proc
Overview
Defines the type of a proc (a ruby callable). At runtime, only validates that the value is a ‘::Proc`.
At present, we only support fixed-arity procs with no optional or keyword arguments.
Instance Method Summary collapse
- #arg_types ⇒ Object
- #build_type ⇒ Object
-
#initialize(arg_types, returns) ⇒ Proc
constructor
A new instance of Proc.
-
#name ⇒ Object
overrides Base.
- #returns ⇒ Object
-
#valid?(obj) ⇒ Boolean
overrides Base.
Methods inherited from Base
#==, #describe_obj, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #recursively_valid?, #subtype_of?, #to_s, #validate!
Constructor Details
#initialize(arg_types, returns) ⇒ Proc
Returns a new instance of Proc.
11 12 13 14 |
# File 'lib/types/types/proc.rb', line 11 def initialize(arg_types, returns) @inner_arg_types = arg_types @inner_returns = returns end |
Instance Method Details
#arg_types ⇒ Object
16 17 18 19 20 |
# File 'lib/types/types/proc.rb', line 16 def arg_types @arg_types ||= @inner_arg_types.transform_values do |raw_type| T::Utils.coerce(raw_type) end end |
#build_type ⇒ Object
26 27 28 29 30 |
# File 'lib/types/types/proc.rb', line 26 def build_type arg_types returns nil end |
#name ⇒ Object
overrides Base
33 34 35 36 37 38 39 |
# File 'lib/types/types/proc.rb', line 33 def name args = [] arg_types.each do |k, v| args << "#{k}: #{v.name}" end "T.proc.params(#{args.join(', ')}).returns(#{returns})" end |
#returns ⇒ Object
22 23 24 |
# File 'lib/types/types/proc.rb', line 22 def returns @returns ||= T::Utils.coerce(@inner_returns) end |
#valid?(obj) ⇒ Boolean
overrides Base
42 43 44 |
# File 'lib/types/types/proc.rb', line 42 def valid?(obj) obj.is_a?(::Proc) end |