|
From: Jonathan P. <jp...@dc...> - 2005-04-12 09:39:11
|
Hi,
After following the instructions on the FAQ [1], with a few fiddles, I
have got Cocoa bindings on a Ruby object to work well. Please let me
know what you think of these modifications - if they seem ok I'll try
to update the Wiki FAQ page.
The main thing I wanted to support was notification from Ruby to Cocoa
upon changing a value in Ruby.
I created the NSObject+RubyCocoaBindings.[mh] and ObjcID.rb files,
adjusting the latter slightly. I've attached the modified ObjcID.rb
file to this message. The result is a model object like this:
class MyModel < OSX::NSObject
kvc_accessor :name, :title
end
The kvc_accessor meta-method takes care of wrapping the setter (key=)
with the Cocoa bindings notifications. If the setter is already
defined, it will be wrapped by the kvc_accessor:
class MyModel < OSX::NSObject
def foo=(value)
@foo = value
@foo_parsed = ... # do some parsing of foo
end
kvc_accessor :foo # wraps foo= with notification, also defines foo
reader method
end
Now, in other code, if you do 'model.foo = "Something"', any GUI
objects bound will automatically update.
Thanks
Jonathan
[1] http://rubycocoa.sourceforge.net/w.en/FAQ.html
|