Module: OSX::OCObjWrapper

Includes:
NSKeyValueCodingAttachment
Included in:
OCClsWrapper
Defined in:
src/ruby/osx/objc/oc_import.rb,
src/ruby/osx/objc/oc_wrapper.rb,
src/objc/mdl_objwrapper.m,
src/objc/mdl_objwrapper.m

Overview

Utilities for communicating Objective-C classes.

Objective-C classes in Ruby world includes OCObjWrapper and extends OCClsWrapper.

Instance Method Summary (collapse)

Methods included from NSKeyValueCodingAttachment

#rbSetValue_forKey, #rbValueForKey

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(mname, *args)

Overrides BasicObject#method_missing.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'src/ruby/osx/objc/oc_wrapper.rb', line 52

def method_missing(mname, *args)
  m_name, m_args, as_predicate = analyze_missing(mname, args)
  begin
    result = self.ocm_send(m_name, mname, as_predicate, *m_args)
  rescue OCMessageSendException => e
    if self.private_methods.include?(mname.to_s)
      raise NoMethodError, "private method `#{mname}' called for ##{self}"
    else
      raise e
    end 
  end
end

Instance Method Details

- (String) objc_method_type

Returns Objective-C type encodings of a given selector.

Examples:

str = OSX::NSString.stringWithString('RubyCocoa')
str.objc_method_type('compare:options:') => "q32@0:8@16Q24"
str.objc_method_type('compare_options_') => "q32@0:8@16Q24"
str.objc_method_type('hasPrefix:') => "c24@0:8@16"

Parameters:

Returns:



661
662
663
664
665
666
667
668
669
670
671
672
# File 'src/objc/mdl_objwrapper.m', line 661

static VALUE
wrapper_objc_method_type (VALUE rcv, VALUE name)
{
  id oc_rcv;
  const char* str;

  rbobj_to_nsobj(rcv, &oc_rcv);
  name = _name_to_selstr (name);
  str = _objc_method_type (object_getClass(oc_rcv), StringValuePtr(name));
  if (str == NULL) return Qnil;
  return rb_str_new2(str);
}

- (Array) objc_methods

Returns an array of Objective-C methods of the reciever. Like Object#methods.

Returns:



576
577
578
579
580
581
582
583
584
585
586
# File 'src/objc/mdl_objwrapper.m', line 576

static VALUE
wrapper_objc_methods (VALUE rcv)
{
  VALUE ary;
  id oc_rcv;

  ary = rb_ary_new();
  rbobj_to_nsobj(rcv, &oc_rcv);
  _ary_push_objc_methods (ary, object_getClass(oc_rcv), 1);
  return ary;
}

- (Object) objc_send(*args)

A convenience method to dispatch a message to ObjC as symbol/value/… For example, [myObj doSomething:arg1 withObject:arg2] would translate as: myObj.objc_send(:doSomething, arg1, :withObject, arg2)

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'src/ruby/osx/objc/oc_wrapper.rb', line 36

def objc_send(*args)
  raise ArgumentError, "wrong number of arguments (#{args.length} for at least 1)" if args.empty?
  mname = ""
  margs = []
  args.each_with_index do |val, index|
    if index % 2 == 0 then
      mname << val.to_s << ':'
    else
      margs << val
    end
  end
  mname.chomp!(':') if args.size == 1
  return self.ocm_send(mname.to_sel, nil, false, *margs)
end

- (Boolean) ocm_respond_to? Also known as: ocm_responds?

Tests whether the reciever responds to a given Objective-C selector.

Parameters:

  • sel (String)

    ruby-style method name, like “foo_bar_”.

Returns:

  • (Boolean)


498
499
500
501
502
503
504
505
# File 'src/objc/mdl_objwrapper.m', line 498

static VALUE
wrapper_ocm_respond_to_p(VALUE rcv, VALUE sel)
{
  id oc_rcv;
  SEL oc_sel = rbobj_to_nssel(sel);
  rbobj_to_nsobj(rcv, &oc_rcv);
  return [oc_rcv respondsToSelector: oc_sel] ? Qtrue : Qfalse;
}

- (Object) ocm_send

Sends Objetive-C message to the reciever.



523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'src/objc/mdl_objwrapper.m', line 523

static VALUE
wrapper_ocm_send(int argc, VALUE* argv, VALUE rcv)
{
  VALUE result;
  VALUE exc;
  exc = ocm_send(argc, argv, rcv, &result);
  if (!NIL_P(exc)) {
    if (exc == Qfalse)
      exc = rb_err_new(ocmsgsend_err_class(), "cannot forward message.");
    rb_exc_raise (exc);
  }
  return result;
}

- (Boolean) ocnil?

Returns whether the Objective-C object of reciever is nil or not.

Returns:

  • (Boolean)


66
67
68
# File 'src/ruby/osx/objc/oc_wrapper.rb', line 66

def ocnil?
  self.__ocid__ == 0
end

- (NSString) to_s

Returns:

  • (NSString)


540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'src/objc/mdl_objwrapper.m', line 540

static VALUE
wrapper_to_s (VALUE rcv)
{
  VALUE ret;
  id oc_rcv;
  id pool;

  rbobj_to_nsobj(rcv, &oc_rcv);
  pool = [[NSAutoreleasePool alloc] init];
  oc_rcv = [oc_rcv description];
  ret = ocstr_to_rbstr(oc_rcv);
  [pool release];
  return ret;
}