【问题标题】:Mark deprecated method ussing ActiveSupport::Deprecation使用 ActiveSupport::Deprecation 标记已弃用的方法
【发布时间】:2011-08-25 00:46:15
【问题描述】:

this question 中解释了如何做一个简单的警告(但不记录跟踪所以不是那么有用)并且有很多方法可以做到这一点,但我没有找到指南。

如何使用 ActiveSupport::Deprecation 将 old_method 标记为已弃用并调用其他 new_method。

【问题讨论】:

    标签: ruby-on-rails ruby deprecated activesupport


    【解决方案1】:

    您可能想查看lib/active_support/deprecation/method_wrappers.rb 的示例。

    【讨论】:

    • 什么意思?这是一个文件,它显示了 Rails 如何使用弃用。本质上,您在代码中需要的是调用 ActiveSupport::Deprecation.deprecate_methods(target_module, *method_names),仅此而已。
    • 这个文件没有解释如何给出选项,我意识到如果我使用ActiveSupport::Deprecation.deprecate_methods(target_module, :deprecated_method => :new_method)它在给出的消​​息中使用新方法。还有其他选择,但没有很好的文档记录。谢谢。
    【解决方案2】:

    正如Roman 所说,可以通过 ActiveSupport::Deprecation.deprecate_methods(target_module, *deprecated_methods)

    地点:

    • target_module 是方法所属的模块或类。
    • deprecated_methods 是一个符号数组。

    在最后一个方法中可以给定选项来自定义弃用消息。

    ActiveSupport::Deprecation.deprecate_methods(target_module, :old_method, \
        :other_old_method => :new_method, :another_old_method => "custom message")
    

    此示例显示调用 old_method 时的默认消息,在第二个示例中注释“use new_method instead”,以及带有 :another_old_method 的自定义消息。

    注意事项:不推荐使用的方法应该定义(之前)并将被执行。 :new_method 不会自动调用。 (还有更多选择,但我不知道)

    【讨论】:

      猜你喜欢
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2012-02-20
      • 2011-11-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多