Method: Typedeaf::ClassMethods#future

Defined in:
lib/typedeaf/classmethods.rb

#future(method_sym, params = {}, primitive = Concurrent::Future, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/typedeaf/classmethods.rb', line 16

def future(method_sym, params={}, primitive=Concurrent::Future, &block)
  __typedeaf_validate_body_for(method_sym, block)
  __typedeaf_method_parameters__[method_sym] = params

  define_method(method_sym) do |*args, &blk|
    __typedeaf_handle_nested_block(params, args, blk)
    __typedeaf_handle_default_parameters(params, args)
    __typedeaf_validate_positionals(params, args)

    stack_element =  [method_sym, __typedeaf_validate_types(params, args)]
    primitive.new do
      # We're inserting into the varstack within the future to make sure
      # we're using the right thread+instance combination
      __typedeaf_varstack__ << stack_element
      begin
        instance_exec(&block)
      ensure
        __typedeaf_varstack__.pop
      end
    end.execute
  end

  return self
end