Class: Bg::Asyncable

Inherits:
Object
  • Object
show all
Defined in:
lib/bg/asyncable.rb

Defined Under Namespace

Modules: Behavior Classes: Wrapper

Instance Method Summary collapse

Constructor Details

#initialize(object, wait: 0) ⇒ Asyncable

Returns a new instance of Asyncable.



32
33
34
35
# File 'lib/bg/asyncable.rb', line 32

def initialize(object, wait: 0)
  @object = object
  @wait = wait.to_f
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bg/asyncable.rb', line 37

def method_missing(name, *args)
  if @object.respond_to? name
    raise ::ArgumentError.new("blocks are not supported") if block_given?
    begin
      wrapped = ::Bg::Asyncable::Wrapper.new(@object, wait: @wait)
      wrapped.async.invoke_method name, *args
    rescue ::StandardError => e
      raise ::ArgumentError.new("Failed to execute method asynchronously! <#{@object.class.name}##{name}> #{e.message}")
    ensure
      return
    end
  end
  super
end

Instance Method Details

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/bg/asyncable.rb', line 52

def respond_to?(name)
  return true if @object.respond_to? name
  super
end