Ruby 1.8.7:拦截对象的链式方法
我有一个类,它包装任意数据的单元格;有点像过滤器。这些单元位于后端数据存储中。但这应该尽可能透明。
编写简单的访问器非常简单:
def foo
# fetch backend cell value and return it
end
def foo=(val)
# store val in backend cell
end
我发现棘手的部分是拦截和跟踪方法,如果数据未被包装,这些方法通常会影响数据。例如,如果数据是数组,则 obj.foo << 17 会将一个元素添加到数组中原位。我想在后端存储的数据上维护这种行为(ie,obj.foo << 17
导致存储的值也添加了一个元素) 。我想也许 method_missing
会有所帮助:
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
但是与 reader 访问器结合使用时,对操作的控制已经超出了我的范围,因为它返回的东西不是东西本身。 (即,如果后端数据是一个数组,我将返回它的副本,并且该副本正在被修改并且永远不会发送回给我。
)这可能吗?如果是这样,我该怎么办? (这可能是非常明显的,我只是想念它,因为我累了——或者也许不累。:-)
谢谢!
[编辑]
换句话说..#method_missing
允许您挂钩未知方法的调用过程。我正在寻找一种类似地挂钩调用过程的方法,但对于所有方法,已知和未知。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将类返回的每个对象包装在一个元对象中,该元对象了解后端,并可以根据需要更新它。
在您的示例中,您需要返回一个可以处理插入、删除等的数组包装对象。
--- 编辑 ---
您可以添加一个“单例方法”,而不是创建大量包装类。返回的对象,特别是如果您可以轻松识别可能需要特殊处理的方法。
我认为任何基于方法缺失的方法都不会起作用,因为您返回的对象确实具有相关的方法。 (即数组确实有一个运算符<<,它没有丢失)或者,也许您可以使用
method_missing
做一些事情,就像您概述的那样。创建一个像这样的元对象:
然后
foo
返回一个DBObject.new(objectFromDB, referenceToDB)
。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.
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:
Then
foo
returns aDBObject.new(objectFromDB, referenceToDB)
.我通过借用 Delegator 模块解决了这个问题。 (下面的代码不保证工作;我已经手动编辑了一些细节。但它应该提供要点。)
在获取(读取器访问器)时,将值注释为使用修改后的方法传回:
在存储(写入器访问器)上,剥离我们添加的单例方法:
因此,当请求该值时,它会在返回之前添加“包装”方法,并且在执行任何操作之前删除单例存储在后端。任何更改该值的操作也会作为副作用更新后端。
目前实施的这项技术有一些不幸的副作用。假设带有包装变量的类在
backend
中实例化,并且通过ivar_foo
访问其中一个变量:但这更多的是目前对我来说好奇心比问题更重要。 :-)
感谢您的帮助和建议!
I solved this by borrowing from the
Delegator
module. (Code that follows is not guaranteed to work; I've edited out some details by hand. But it should supply the gist.)On a fetch (reader accessor), annotate the value to be passed back with modified methods:
On a store (writer accessor), strip the singleton methods we added:
So when the value is requested, it gets 'wrapper' methods added to it before being returned, and the singletons are removed before anything is stored in the back end. Any operations that change the value will also update the back end as a side-effect.
There are some unfortunate side-side-effects of this technique as currently implemented. Assume that the class with the wrapped variables is instantiated in
backend
, and that one of the variables is accessed viaivar_foo
:But that's more of a curiousity than a problem for me at the moment. :-)
Thanks for the help and suggestions!