delegate also from the inside

F

Florian Weber

hi!

if i use a proxy to wrap a object, is there a way to allow that calls
from the wrapped object to a method in the wrapped object go via
the proxy.

example:

proxy#foo calls object#foo calls proxy#bar calls object#bar

instead of

proxy#foo calls object#foo calls object#bar

thanks for any tipp!

ciao!
florian
 
R

Robert Klemme

Florian Weber said:
hi!

if i use a proxy to wrap a object, is there a way to allow that calls
from the wrapped object to a method in the wrapped object go via
the proxy.

example:

proxy#foo calls object#foo calls proxy#bar calls object#bar

instead of

proxy#foo calls object#foo calls object#bar

No way other than modifying the wrapped instance. The wrapper knows the
wrapped instance but not the other way round. You might consider using
inheritance in this case - if that works in your scenario. Or use a
completely different strategy.

robert
 
F

Florian Weber

No way other than modifying the wrapped instance. The wrapper knows
the
wrapped instance but not the other way round. You might consider using
inheritance in this case - if that works in your scenario. Or use a
completely different strategy.

what ways via modifications would work? besides using inheritance?
 
R

Robert Klemme

Florian Weber said:
what ways via modifications would work? besides using inheritance?

Overriding methods in the instance to wrap. You could do that
automatically to move every method to xyz_unwrapped and subsitute it with
def xyz; wrapper.xyz; end while the wrapper invokes always
wrapped.xyz_unwrapped(). Quite complicated though.

Or you write the class to be wrapped from the start to take wrapping into
account if that's possible. Something like

class Wrappable
attr_accessor :wrapper

def me; wrapper or self; end

def some_method
me.some_other_method
end

def some_other_method
# ...
end
end

Other than that I'd have to know the scenario in order to come up with
additional suggestions. I guess these solutions then would not make use
of wrapping. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,843
Members
47,390
Latest member
RobertMart

Latest Threads

Top