Class: T::Types::Proc

Inherits:
Base
  • Object
show all
Defined in:
lib/types/types/proc.rb

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

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_typesObject



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_typeObject



26
27
28
29
30
# File 'lib/types/types/proc.rb', line 26

def build_type
  arg_types
  returns
  nil
end

#nameObject

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

#returnsObject



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

Returns:



42
43
44
# File 'lib/types/types/proc.rb', line 42

def valid?(obj)
  obj.is_a?(::Proc)
end