Module: OSX::NSBehaviorAttachment

Included in:
OCClsWrapper
Defined in:
src/ruby/osx/objc/oc_import.rb

Overview

Defines Objecitve-C objects' behaviors.

Constant Summary

ERRMSG_FOR_RESTRICT_NEW =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

An error message.

"use 'alloc.initXXX' to instantiate Cocoa Object"
OCTYPES =

TODO: support more types such as pointers…

{
  :id       => '@',
  :class    => '#',
  :BOOL     => 'c',
  :char     => 'c',
  :uchar    => 'C',
  :short    => 's',
  :ushort   => 'S',
  :int      => 'i',
  :uint     => 'I',
  :long     => 'l',
  :ulong    => 'L',
  :float    => 'f',
  :double   => 'd',
  :bool     => 'B',
  :void     => 'v',
  :selector => ':',
  :sel      => ':',
  :longlong => 'q',
  :ulonglong => 'Q',
  :cstr     => '*',
}

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) _no_param_method?(typefmt)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether given typefmt do not contains any parameters.

Returns:

  • (Boolean)


424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'src/ruby/osx/objc/oc_import.rb', line 424

def _no_param_method?(typefmt)
  if typefmt[0] == ?{
    count = 1
    i = 0
    while count > 0 and i = typefmt.index(/[{}]/, i + 1)
      case typefmt[i]
      when ?{; count += 1
      when ?}; count -= 1
      end
    end
    raise ArgumentError, "illegal type encodings" unless i
    typefmt[i+1..-1] == '@:'
  else
    typefmt.index('@:') == typefmt.length - 2
  end
end

- (Object) _ns_behavior_method_added(sym, class_method)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Overrides automatically Objective-C methods.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'src/ruby/osx/objc/oc_import.rb', line 397

def _ns_behavior_method_added(sym, class_method)
  return if OSX._ignore_ns_override
  sel = sym.to_s.gsub(/([^_])_/, '\1:')
  m = class_method ? method(sym) : instance_method(sym)
  arity = m.arity
  sel << ':' if arity > 0 and /[^:]\z/ =~ sel
  mtype = nil
  if _ns_enable_override?(sel, class_method) or
  mtype = OSX.lookup_informal_protocol_method_type(sel, class_method)
    expected_arity = sel.scan(/:/).length
    if expected_arity != arity
      raise RuntimeError, "Cannot override Objective-C method '#{sel}' with Ruby method ##{sym}, they should both have the same number of arguments. (expected arity #{expected_arity}, got #{arity})"
    end
    OSX.objc_class_method_add(self, sel, class_method, mtype)
  end
end

- (Boolean) _ns_enable_override?(sel, class_method)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether override Objective-C methods with given name is enable or not.

Parameters:

  • sel (Symbol, String)
  • class_method (Boolean)

Returns:

  • (Boolean)


418
419
420
# File 'src/ruby/osx/objc/oc_import.rb', line 418

def _ns_enable_override?(sel, class_method)
  ns_inherited? and (class_method ? self.objc_method_type(sel) : self.objc_instance_method_type(sel))
end

- (Object) _objc_export(name, types, class_method)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds an method to Objective-C class.



443
444
445
446
447
448
449
# File 'src/ruby/osx/objc/oc_import.rb', line 443

def _objc_export(name, types, class_method)
  typefmt = _types_to_typefmt(types)
  name = name.to_s
  name = name[0].chr << name[1..-1].gsub(/_/, ':')
  name << ':' if name[-1] != ?: and not _no_param_method?(typefmt)
  OSX.objc_class_method_add(self, name, class_method, typefmt)
end

- (Object) _types_to_typefmt(types)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'src/ruby/osx/objc/oc_import.rb', line 510

def _types_to_typefmt(types)
  return types.strip if types.is_a?(String)
  raise ArgumentError, "Array or String (as type format) expected (got #{types.klass} instead)" unless types.is_a?(Array)
  raise ArgumentError, "Given types array should have at least an element" unless types.size > 0
  octypes = types.map do |type|
    if type.is_a?(Class) and type.ancestors.include?(OSX::Boxed)
      type.encoding
    else
      type = type.strip.intern unless type.is_a?(Symbol)
      octype = OCTYPES[type]
      raise "Invalid type (got '#{type}', expected one of : #{OCTYPES.keys.join(', ')}, or a boxed class)" if octype.nil?
      octype
    end
  end
  octypes[0] + '@:' + octypes[1..-1].join
end

- (Object) def_objc_method(name, types, &blk)

Defines Objective-C instance method.



452
453
454
455
456
457
458
# File 'src/ruby/osx/objc/oc_import.rb', line 452

def def_objc_method(name, types, &blk)
  if block_given? then
    objc_method(name, types, &blk)
  else
    raise ArgumentError, "block for method implementation expected"
  end
end

- (Object) ib_action(name, &blk)

declare a IBAction method. if given a block, it mean the implementation of the action.



391
392
393
# File 'src/ruby/osx/objc/oc_import.rb', line 391

def ib_action(name, &blk)
  define_method(name, blk) if block_given?
end

- (Object) ib_outlets(*args) Also known as: ib_outlet

declare write-only attribute accessors which are named IBOutlet in the Objective-C world.



377
378
379
# File 'src/ruby/osx/objc/oc_import.rb', line 377

def ib_outlets(*args)
  attr_writer(*args)
end

- (Object) new

restrict creating an instance by Class#new, unless the Objective-C class really responds to the new selector.



335
336
337
338
339
340
341
# File 'src/ruby/osx/objc/oc_import.rb', line 335

def new
  if ocm_respond_to?(:new)
    objc_send(:new)
  else
    raise ERRMSG_FOR_RESTRICT_NEW
  end
end

- (Boolean) ns_inherited

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

initializer for definition of a derived class of a class on Objective-C World.

Returns:

  • (Boolean)


347
348
349
350
351
352
353
354
355
356
357
# File 'src/ruby/osx/objc/oc_import.rb', line 347

def ns_inherited()
  return if ns_inherited?
  kls_name = self.name.to_s.split('::')[-1]
  if kls_name
    spr_name = superclass.name.split('::')[-1]
    occls = OSX.objc_derived_class_new(self, kls_name, spr_name)
    self.instance_eval { @ocid = occls.__ocid__.to_i }
    OSX::BundleSupport.bind_class_with_current_bundle(self) 
  end
  @inherited = true
end

- (Boolean) ns_inherited?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether receiver class is inherited from Objecitve-C class or not.

Returns:

  • (Boolean)


361
362
363
# File 'src/ruby/osx/objc/oc_import.rb', line 361

def ns_inherited?
  return defined?(@inherited) && @inherited
end

- (Object) ns_outlets(*args) Also known as: ns_outlet

Deprecated.

use ib_outlet.



383
384
385
386
# File 'src/ruby/osx/objc/oc_import.rb', line 383

def ns_outlets(*args)
  warn "#{caller[0]}:: ns_outlet(s) is deprecated, and will be removed in a next release. Please use ib_outlet(s) instead."
  ib_outlets(*args)
end

- (Object) ns_overrides(*args) Also known as: ns_override, ib_override, ib_overrides

Deprecated.

DO NOT use. RubyCocoa automatically overrides Objective-C methods.

declare to override instance methods of super class which is defined by Objective-C.



368
369
370
# File 'src/ruby/osx/objc/oc_import.rb', line 368

def ns_overrides(*args)
  warn "#{caller[0]}: ns_overrides is no longer necessary, should not be called anymore and will be removed in a next release. Please update your code to not use it."
end

- (Object) objc_alias_class_method(new, old)

Aliases Objective-C classs method.



480
481
482
483
484
# File 'src/ruby/osx/objc/oc_import.rb', line 480

def objc_alias_class_method(new, old)
  new_sel = new.to_s.gsub(/([^_])_/, '\1:')
  old_sel = old.to_s.gsub(/([^_])_/, '\1:')
  _objc_alias_class_method(new, old)
end

- (Object) objc_alias_method(new, old)

Aliases Objective-C method.



473
474
475
476
477
# File 'src/ruby/osx/objc/oc_import.rb', line 473

def objc_alias_method(new, old)
  new_sel = new.to_s.gsub(/([^_])_/, '\1:')
  old_sel = old.to_s.gsub(/([^_])_/, '\1:')
  _objc_alias_method(new, old)
end

- (Object) objc_class_method(name, types = ['id'])

Defines Objective-C class method.



468
469
470
# File 'src/ruby/osx/objc/oc_import.rb', line 468

def objc_class_method(name, types=['id'])
  _objc_export(name, types, true)
end

- (Object) objc_method(name, types = ['id'], &blk)

Defines Objective-C instance method.

Parameters:

  • name


462
463
464
465
# File 'src/ruby/osx/objc/oc_import.rb', line 462

def objc_method(name, types=['id'], &blk)
  define_method(name, blk) if block_given?
  _objc_export(name, types, false)
end