Module: Typedeaf::ClassMethods

Defined in:
lib/typedeaf/classmethods.rb

Instance Method Summary collapse

Instance Method Details

#__typedeaf_method_parameters__Object



70
71
72
73
74
75
76
# File 'lib/typedeaf/classmethods.rb', line 70

def __typedeaf_method_parameters__
  if @__typedeaf_method_parameters__.nil?
    @__typedeaf_method_parameters__ = {}
  end

  return @__typedeaf_method_parameters__
end

#default(value, *types) ⇒ Object



8
9
10
# File 'lib/typedeaf/classmethods.rb', line 8

def default(value, *types)
  return Typedeaf::Arguments::DefaultArgument.new(value, *types)
end

#define(method_sym, params = {}, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/typedeaf/classmethods.rb', line 41

def define(method_sym, params={}, &block)
  params = params.freeze
  __typedeaf_validate_body_for(method_sym, block)
  __typedeaf_method_parameters__[method_sym] = params

  define_method(method_sym) do |*args, &blk|
    # Optimization, if we're a parameter-less method, just pass right
    # through without any checks whatsoever
    if params.empty?
      return instance_exec(&block)
    end

    __typedeaf_handle_nested_block(params, args, blk)
    __typedeaf_handle_default_parameters(params, args)
    __typedeaf_validate_positionals(params, args)

    __typedeaf_varstack__ << [method_sym,
                              __typedeaf_validate_types(params, args)]

    begin
      instance_exec(&block)
    ensure
      __typedeaf_varstack__.pop
    end
  end

  return self
end

#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

#promise(method_sym, params = {}, &block) ⇒ Object



12
13
14
# File 'lib/typedeaf/classmethods.rb', line 12

def promise(method_sym, params={}, &block)
  future(method_sym, params, primitive=Concurrent::Promise, &block)
end