使用Rails演示者-可记忆性的3.1版本已弃用-使用|| =代替? [英] Using rails presenters - memoizable getting deprecated in 3.1 - use ||= instead?

查看:88
本文介绍了使用Rails演示者-可记忆性的3.1版本已弃用-使用|| =代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:为避免在可能的情况下创建多个对象或多个查询.

我正在使用带有导轨的Presenters作为最佳实践.

我正在遵循这样的建议:由于一些问题,最好在设置具有@the_record = record ||= @record样式的项目时使用扩展ActiveSupport.Memoizable"(然后记住:方法以使用它们) -不会存储为false或nil,因此查询将再次被调用,并且备忘录化功能可以更好地使用缓存(即使用它!).

但是我发现在Rails 3.1中已不再使用备忘录式的了 注意我在github上的载波和声明: 不推荐使用警告:ActiveSupport :: Memoizable已被弃用,并将在以后的版本中删除,而仅使用Ruby备注模式.(从/Users/kain/.rvm/gems/ruby-1.9.3-extend中调用gems/carrierwave-c4459179b0f8/lib/carrierwave/mount.rb:284.

也许已经解决了吗?有人知道吗?

是否有关于最佳实践的建议?使用|| =语法?那么上述问题呢?

解决方案

||=方法非常适用于返回评估结果为true的值的情况,但不适用于那些返回值为true的情况. memoize通过捕获这些条件并相应地返回来解决此问题.如果您想容纳nil:

,则可以采用这样的方法:

def some_method
  return @some_method if (instance_variable_defined?(:"@some_method"))

  @some_method = begin
    ...
  end
end

这只是检查变量是否已定义,而不是是否已设置,这在您的情况下是重要的区别.

我不确定您为什么认为它已被弃用[Michael的注释,在3.2中已弃用,请参见下面的注释]. 文档表示该文档在3.1中仍然有效.有时,当将实现从一个模块移动到另一个模块时,这些实现被标记为已弃用",但是该工具仍然可用.

Issue: To avoid creating multiple objects or multiple queries when possible.

I am using Presenters with rails as a Best Practice.

I am following advice that says that it would be good to use "extend ActiveSupport.Memoizable" (and then memoize :method(s) to use them) over setting up items with @the_record = record ||= @record style because of a couple of issues - false or nil not getting stored so the query gets called again and also that memoizable uses the cache better (i.e. uses it!).

However I see that memoizable is getting deprecated in rails 3.1 Notes i github under carrierwave and with statement: "DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases,simply use Ruby memoization pattern instead. (called from extend at /Users/kain/.rvm/gems/ruby-1.9.3-preview1/bundler/gems/carrierwave-c4459179b0f8/lib/carrierwave/mount.rb:284".

Maybe it's been resolved though? Anyone know?

Any suggestions about the best practice to use going forward? Use the ||= syntax? What about the above issues?

解决方案

The ||= method is great for things that return values that evaluate as true, but it doesn't work very well for things that don't. memoize does work around this by trapping these conditions and returning accordingly. You might take an approach like this if you want to accommodate nil:

def some_method
  return @some_method if (instance_variable_defined?(:"@some_method"))

  @some_method = begin
    ...
  end
end

This just checks if the variable is defined, not if it is set, which is an important distinction in your case.

I'm not sure why you think it's being deprecated [Note from Michael, it's deprecated in 3.2, see note below]. The documentation indicates it's still current in 3.1. Sometimes implementations are marked as "deprecated" when they're being moved from one module to another, but the facility remains available.

这篇关于使用Rails演示者-可记忆性的3.1版本已弃用-使用|| =代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆