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 Attribute Summary collapse

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.



14
15
16
17
18
19
20
# File 'lib/types/types/proc.rb', line 14

def initialize(arg_types, returns)
  @arg_types = {}
  arg_types.each do |key, raw_type|
    @arg_types[key] = T::Utils.coerce(raw_type)
  end
  @returns = T::Utils.coerce(returns)
end

Instance Attribute Details

#arg_typesObject (readonly)

Returns the value of attribute arg_types.



11
12
13
# File 'lib/types/types/proc.rb', line 11

def arg_types
  @arg_types
end

#returnsObject (readonly)

Returns the value of attribute returns.



12
13
14
# File 'lib/types/types/proc.rb', line 12

def returns
  @returns
end

Instance Method Details

#nameObject

overrides Base



23
24
25
26
27
28
29
# File 'lib/types/types/proc.rb', line 23

def name
  args = []
  @arg_types.each do |k, v|
    args << "#{k}: #{v.name}"
  end
  "T.proc.params(#{args.join(', ')}).returns(#{returns})"
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



32
33
34
# File 'lib/types/types/proc.rb', line 32

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