|
From: Jonathan P. <jp...@dc...> - 2005-11-19 15:54:44
|
On 18 Nov 2005, at 23:00, Rupert BARROW wrote:
> - I cannot programmatically (from Ruby) add objects to the Notes
> controller to get them inserted into GUI
I can't see any code that tries to do that. Can you indicate what
you've tried?
I've figured out the problem with the key-value coding. You can make
the following simplifications to the code:
(1) no need for 'kvc_wrapper'
(2) no need to call kvc_wrapper_representation at all
(3) no need for @_changed instance variable or corresponding methods
Important bit - change this:
> def valueForUndefinedKey(k)
> @_representation.valueForKey(k)
> end
>
> def setValue_ForUndefinedKey(v, k)
> @_representation.setValueForKey(v, k)
> end
to this:
def rbValueForKey(k)
@_representation.valueForKey(k)
end
def rbSetValue_forKey(v,k)
self.willChangeValueForKey(k)
@_representation.setValueForKey(v,k)
self.didChangeValueForKey(k)
end
Here's why:
RubyCocoa (in OverrideMixin.m) overrides valueForUndefinedKey: and
setValue:forUndefinedKey: and passes them through to rbValueForKey
and rbSetValue_forKey, which are defined in oc_import.rb.
The definition in oc_import.rb looks for ruby-like setters/getters
and calls them, calling the willChange/didChange... methods when
necessary also.
So, for example, [obj setValue: x forUndefinedKey: @"y"] ends up
calling 'obj.y = x' in ruby-land.
Since the valueForUndefinedKey: and setValue... methods are defined
at a low level in OverrideMixin.m, the original definition in Note.rb
never took effect. By overriding instead the ruby delegates
(rbValue.. etc) we can achieve the desired effect.
I think it may be neater in the long-run to make the kvc_accessor
methods in oc_import.rb define real methods (using
addRubyMethod_withType) and avoid the undefined key hack altogether.
I'll have a look into this.
Hope that makes some sense,
Jonathan
|