Module: AsyncMagic::ClassMethods

Defined in:
lib/async_magic/async.rb

Instance Method Summary collapse

Instance Method Details

#asyncObject



28
29
30
# File 'lib/async_magic/async.rb', line 28

def async
  @async_next_method = true
end

#method_added(method_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/async_magic/async.rb', line 53

def method_added(method_name)
  super
  if defined?(@async_next_method) && @async_next_method
    @async_next_method = false

    original_method = instance_method(method_name)
    define_method(method_name) do |*args, &block|
      future = Concurrent::Future.execute(executor: THREAD_POOL) do
        original_method.bind_call(self, *args, &block)
      rescue => e
        ASYNC_LOGGER.error("Async method #{method_name} failed: #{e.message}")
        ASYNC_LOGGER.error(e.backtrace.join("\n"))
        raise e
      end
      future
    end

    async_methods_list << method_name
  end
end

#singleton_method_added(method_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/async_magic/async.rb', line 32

def singleton_method_added(method_name)
  super
  if defined?(@async_next_method) && @async_next_method
    @async_next_method = false

    original_method = method(method_name)
    define_singleton_method(method_name) do |*args, &block|
      future = Concurrent::Future.execute(executor: THREAD_POOL) do
        original_method.call(*args, &block)
      rescue => e
        ASYNC_LOGGER.error("Async class method #{method_name} failed: #{e.message}")
        ASYNC_LOGGER.error(e.backtrace.join("\n"))
        raise e
      end
      future
    end

    async_class_methods_list << method_name
  end
end