gpt4 book ai didi

ruby-on-rails - 如何在未知类型对象的对象实例上使用 define_method?

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:27 26 4
gpt4 key购买 nike

所以,我有点想做一些类似于 rspec/mocha 的 mock 的事情。 ,但仅适用于两个对象,而不是所有对象。这是我目前所拥有的:

def mock(obj, method_to_mock, value)
obj.class << obj do
define_method(method_to_mock) do
return value
end
end
end

我从这篇文章中得到这样写的想法:https://stackoverflow.com/a/185969/356849

然后我可以做这样的事情:

mock(self.instantiated, :sections, sections)

它会覆盖我存储在 self.instantiated 中的对象的 sections使用我的 Section 对象数组,sections .

我这样做的原因是因为我正在存储一个对象的序列化和加密版本,我希望能够解密和反序列化该对象,然后恢复所有关系,这样我可以在我的 View 中查看该对象,就好像它是从数据库中读取的一样。但这并不重要,大部分已经完成。

所以,我希望能够做到这一点:

mock(<Instance of object>, :<method of object that is going to be overridden, to avoid db access>, <the stuff to return when the overridden method is invoked)

目前,我在 obj.class << obj do 上遇到错误符合这一点:

NoMethodError: undefined method `obj' for #<MyObject::Encrypted:0x7f190eebcd18>

想法?


更新

将第二行改为class << obj现在无限循环。

from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `retrieve_connection_pool'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `retrieve_connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1305:in `columns'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:1318:in `column_names'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:35:in `ordering_condition_details'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/ordering.rb:26:in `method_missing'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/searchlogic-2.4.28/lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/activerecord-2.3.15/lib/active_record/base.rb:2002:in `method_missing_without_paginate'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/gems/will_paginate-2.3.16/lib/will_paginate/finder.rb:170:in `method_missing_without_attr_encrypted'
from /home/me/.rvm/gems/ruby-1.8.7-p371@project/bundler/gems/attr_encrypted-a4b25f01d137/lib/attr_encrypted/adapters/active_record.rb:50:in `method_missing'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:119:in `mock'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:79:in `instantiate'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `each'
from /home/me/Work/GravityLabs/project/app/models/proposal/encrypted.rb:58:in `instantiate'

最佳答案

def mock(obj, method_to_mock, value=nil)
obj.define_singleton_method(method_to_mock) do value end
end

关于ruby-on-rails - 如何在未知类型对象的对象实例上使用 define_method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14365702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com