Module: Libcouchbase::Callbacks::ClassMethods

Defined in:
lib/libcouchbase/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callback_funcsObject (readonly)

Provide accessor methods to the class level instance variables



36
37
38
# File 'lib/libcouchbase/callbacks.rb', line 36

def callback_funcs
  @callback_funcs
end

#callback_lockObject (readonly)

Provide accessor methods to the class level instance variables



36
37
38
# File 'lib/libcouchbase/callbacks.rb', line 36

def callback_lock
  @callback_lock
end

#callback_lookupObject (readonly)

Provide accessor methods to the class level instance variables



36
37
38
# File 'lib/libcouchbase/callbacks.rb', line 36

def callback_lookup
  @callback_lookup
end

Instance Method Details

#default_lookup(req, *args) ⇒ Object

This function is used to work out the instance the callback is for



40
41
42
# File 'lib/libcouchbase/callbacks.rb', line 40

def default_lookup(req, *args)
    req.address
end

#define_callback(function:, params: [:pointer, :int, :pointer], ret_val: :void, lookup: :default_lookup) ⇒ Object



20
21
22
23
24
# File 'lib/libcouchbase/callbacks.rb', line 20

def define_callback(function:, params: [:pointer, :int, :pointer], ret_val: :void, lookup: :default_lookup)
    @callback_funcs[function] = ::FFI::Function.new(ret_val, params) do |*args|
        dispatch_callback(function, lookup, args)
    end
end

#dispatch_callback(func_name, lookup, args) ⇒ Object



14
15
16
17
18
# File 'lib/libcouchbase/callbacks.rb', line 14

def dispatch_callback(func_name, lookup, args)
    instance_id = __send__(lookup, *args)
    inst = @callback_lookup[instance_id]
    inst.__send__(func_name, *args) if inst.respond_to?(func_name, true)
end

#inherited(subclass) ⇒ Object

Much like include to support inheritance properly We keep existing callbacks and inherit the lookup (as this will never clash)



28
29
30
31
32
# File 'lib/libcouchbase/callbacks.rb', line 28

def inherited(subclass)
    subclass.instance_variable_set(:@callback_funcs, {}.merge(@callback_funcs))
    subclass.instance_variable_set(:@callback_lookup, @callback_lookup)
    subclass.instance_variable_set(:@callback_lock, @callback_lock)
end