Class: CallbackAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/callback-adapter.rb

Overview

Adapter from an callback-based interface to standard interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ CallbackAdapter

Constructor.

Parameters:

  • object (Object)

    some proxied object



23
24
25
# File 'lib/callback-adapter.rb', line 23

def initialize(object)
    @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Methods call catcher. Thread safe.

Parameters:

  • name (Symbol)

    name of the method

  • args (Array)

    call arguments

Returns:

  • (Object)

    returned value or array of returned values



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/callback-adapter.rb', line 35

def method_missing(name, *args)
    returns = nil
    mutex = Mutex::new.lock

    @object.send(name, *args) do |*rets|
        case rets.length
            when 0
                returns = nil
            when 1
                returns = rets.first
            else
                returns = rets
        end
        
        mutex.unlock()
    end
    
    mutex.synchronize do
        return returns
    end
end

Instance Attribute Details

#objectObject

Holds the proxied object.

Returns:

  • (Object)

    the proxied object



15
16
17
# File 'lib/callback-adapter.rb', line 15

def object
  @object
end