Class: Bg::Asyncable

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

Defined Under Namespace

Modules: Behavior Classes: Wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, delay: 0) ⇒ Asyncable

Returns a new instance of Asyncable.

Raises:

  • (::ArgumentError)


35
36
37
38
39
40
# File 'lib/bg/asyncable.rb', line 35

def initialize(object, delay: 0)
  raise ::ArgumentError unless object.is_a?(::GlobalID::Identification)
  super()
  @object = object
  @delay = delay.to_f
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bg/asyncable.rb', line 42

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.to_global_id, delay: delay)
      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 Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



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

def delay
  @delay
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Instance Method Details

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/bg/asyncable.rb', line 57

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