I have a class that is wrapping cells of arbitrary data; sort of a filter. The cells live in a backend datastore. but that should be as transparent as possible.
Writing straightforward accessors is simple enough:
def foo
# fetch backend cell value and return it
end
def foo=(val)
# store val in backend cell
end
The part I'm finding tricky is intercepting and tracking methods that would ordinarily affect the data if it weren't wrapped. For instance, if the data is an array, obj.foo << 17 would add an element to the array in situ. I want to maintain that behaviour on the data stored in the backend (i.e., obj.foo << 17 results in the stored value having an element added as well). I thought perhaps a method_missing would help:
def method_missing(meth, *args)
methsym = meth.to_sym
curval = self.get
lastval = curval.clone
opresult = curval.__send__(methsym, *args)
if (curval != lastval)
self.set(curval)
end
return opresult
end
but in combination with the reader accessor, control of the operation has moved beyond me because the thing it returns is not the thing itself. (I.e., if the backend data is an array, I'm returning a copy of it, and it's the copy that's being modified and never sent back to me.)
Is this possible? If so, how can I do it? (It's probably painfully obvious and I'm just missing it because I'm tired -- or maybe not. :-)
Thanks!
[edited]
To put it another way.. #method_missing allows you to hook into the invocation process for unknown methods. I'm looking for a way to hook into the invocation process similarly, but for all methods, known and unknown.
Thanks!
You'd need to wrap each object returned by your class inside a meta object which is aware of the backend, and could update it as needed.
In your example, you'd need to return an array wrapper object which could handle inserts, deletes, etc.
--- Edit ---
Instead of creating lots of wrapper classes, you may be able to add a 'singleton method' to the returned objects, especially if you can easily identify the methods that might need special handling.
module BackEndIF
alias :old_send :__send__
def __send__ method, *args
if MethodsThatNeedSpecialHandling.include?(method)
doSpecialHandling()
else
old_send(method,args)
end
end
end
#in your class:
def foo
data = Backend.fetch(query)
data.extend(BackEndIF)
return data
end
I don't think anything based on method-missing will work, since the objects you are returning do have the methods in question. (i.e. Array does have an operator<<, it's not missing)
Or, maybe you can do something with a method_missing like the one you outline.
Create a single meta_object something like this:
class DBobject
def initialize(value, db_reference)
@value = value
@ref = db_reference
end
def method_missing(meth, *args)
old_val = @value
result = @value.__send__(meth, *args)
DatabaseUpdate(@ref, @value) if (@value != old_val)
return result
end
end
Then foo returns a DBObject.new(objectFromDB, referenceToDB).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With