Method: Cocoa.attach_method

Defined in:
lib/cocoa/helpers.rb

.attach_method(method, *params) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cocoa/helpers.rb', line 53

def self.attach_method method,*params
  if params.first.is_a? Array
    params = params.last
  end

  m = ObjC::MethodDef.new(method,params.first)

  params = params.extract_options!
  params.freeze
  begin
    if params[:args] == 0
      attach_function "call_#{method}".to_sym, method, m.ffi_types, m.ffi_return_type
      define_method method do
        m.ruby_return_value(send("call_#{method}".to_sym))
      end
    else
      attach_function "call_#{method}".to_sym, method, m.ffi_types, m.ffi_return_type
      define_method method do |*args|
        raise ArgumentError.new("#{method} requires #{params[:args]} argument(s)") unless params[:args] == args.size
        m.ruby_return_value(self,send("call_#{method}".to_sym,*m.call_arguments(args)))
      end
    end
  rescue FFI::NotFoundError => e
  end
end