Class: Object

Inherits:
BasicObject
Defined in:
src/ruby/osx/objc/oc_import_openclass.rb

Overview

The following code defines a new subclass of Object (Ruby's).

 module OSX
   class NSCocoaClass end
 end

This Object.inherited() replaces the subclass of Object class by a Cocoa class from # OSX.ns_import.

Class Method Summary (collapse)

Class Method Details

+ (Object) _before_method_added



80
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 80

alias _before_method_added method_added

+ (Object) _before_osx_inherited



34
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 34

alias _before_osx_inherited inherited

+ (Object) _before_singleton_method_added



86
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 86

alias _before_singleton_method_added singleton_method_added

+ (Object) _real_class_and_mod(klass)



21
22
23
24
25
26
27
28
29
30
31
32
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 21

def _real_class_and_mod(klass)
  unless klass.ancestors.include?(OSX::Boxed)
    klassname = klass.name.to_s
    unless klassname.nil? || klassname.empty?
      if Object.included_modules.include?(OSX) and /::/.match(klassname).nil?
        [klassname, Object]
      elsif klassname[0..4] == 'OSX::' and (tokens = klassname.split(/::/)).size == 2 and klass.superclass != OSX::Boxed
        [tokens[1], OSX]
      end
    end
  end
end

+ (Object) _register_method(sym, class_method)



52
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
78
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 52

def _register_method(sym, class_method)
  if self != Object
    nsklassname, mod = _real_class_and_mod(self)
    if nsklassname
      begin
        nsklass = OSX.const_get(nsklassname)
        raise NameError unless nsklass.ancestors.include?(OSX::NSObject)
        if class_method
          method = self.method(sym).unbind
          OSX.__rebind_umethod__(nsklass.class, method)
          nsklass.module_eval do 
            (class << self; self; end).instance_eval do 
              define_method(sym, method)
            end
          end
        else
          method = self.instance_method(sym)
          OSX.__rebind_umethod__(nsklass, method)
          nsklass.module_eval do
            define_method(sym, method)
          end
        end
      rescue NameError
      end
    end
  end
end

+ (Object) inherited(subklass)



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

def inherited(subklass)
  nsklassname, mod = _real_class_and_mod(subklass) 
  if nsklassname and (first_char = nsklassname[0]) >= ?A and first_char <= ?Z
    # remove Ruby's class
    mod.instance_eval { remove_const nsklassname.intern }
    begin
      klass = OSX.ns_import nsklassname.intern
      raise NameError if klass.nil?
      subklass = klass
    rescue NameError
      # redefine subclass (looks not a Cocoa class)
      mod.const_set(nsklassname, subklass)
    end
  end
  _before_osx_inherited(subklass)
end

+ (Object) method_added(sym)



81
82
83
84
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 81

def method_added(sym)
  _register_method(sym, false)
  _before_method_added(sym)
end

+ (Object) method_missing(symbol, *args)

Raises:

  • (NoMethodError)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 92

def method_missing(symbol, *args)
  nsklassname, mod = _real_class_and_mod(self)
  if nsklassname
    begin
      nsklass = OSX.const_get(nsklassname)
      if nsklass.respond_to?(symbol)
        return nsklass.send(symbol, *args)
      end
    rescue NameError
    end
  end
  raise NoMethodError, "undefined method `#{symbol.to_s}' for #{self}"
end

+ (Object) singleton_method_added(sym)



87
88
89
90
# File 'src/ruby/osx/objc/oc_import_openclass.rb', line 87

def singleton_method_added(sym)
  _register_method(sym, true)
  _before_singleton_method_added(sym)
end