简体   繁体   English

如何为已弃用的 alias_method_chain 重构代码

[英]How to refactor code for deprecated alias_method_chain

I'm upgrading my rails application and I'm getting a warning saying alias_method_chain is deprecated. Please, use Module#prepend instead我正在升级我的 rails 应用程序,我收到一条警告说alias_method_chain is deprecated. Please, use Module#prepend instead alias_method_chain is deprecated. Please, use Module#prepend instead . alias_method_chain is deprecated. Please, use Module#prepend instead But I'm not really understanding how to handle this.但我真的不明白如何处理这个。 How can I change the code below?如何更改下面的代码?

  def read_attribute_with_mapping(attr_name)
    read_attribute_without_mapping(ADDRESS_MAPPING[attr_name] || attr_name)
  end
  alias_method_chain :read_attribute, :mapping

prepend is basically like importing a module, but it ends up "in front" of other code (so the module can call super to run the code it's in front of). prepend基本上就像导入一个模块,但它最终“在”其他代码的“前面”(因此模块可以调用super来运行它前面的代码)。

This is a runnable example with something close to your situation.这是一个可运行的示例,与您的情况很接近。

module MyModule
  def read_attribute(attr_name)
    super("modified_#{attr_name}")
  end
end

class Example
  prepend MyModule

  def read_attribute(attr_name)
    puts "Reading #{attr_name}"
  end
end

Example.new.read_attribute(:foo)
# Outputs: Reading modified_foo

I defined read_attribute directly on Example , but it could just as well have been a method inherited from a superclass (such as ActiveRecord::Base ).我直接在Example上定义了read_attribute ,但它也可以是从超类(例如ActiveRecord::Base )继承的方法。

This is a shorter but more cryptic version that uses an anonymous module:这是一个使用匿名模块的更短但更神秘的版本:

class Example
  prepend(Module.new do
    def read_attribute(attr_name)
      super("modified_#{attr_name}")
    end
  end)

  def read_attribute(attr_name)
    puts "Reading #{attr_name}"
  end
end

Example.new.read_attribute(:foo)
# Outputs: Reading modified_foo

UPDATE:更新:

Just for fun and to address a question below, here's how it could be done without having to explicitly make any modules yourself.只是为了好玩和解决下面的一个问题,这里是如何在不必自己明确制作任何模块的情况下完成的。 I don't think I'd choose to do it this way myself, since it obscures a common pattern.我不认为我自己会选择这样做,因为它掩盖了一个共同的模式。

# You'd do this once somewhere, e.g. config/initializers/prepend_block.rb in a Rails app.
class Module
  def prepend_block(&block)
    prepend Module.new.tap { |m| m.module_eval(&block) }
  end
end

# Now you can do:
class Example
  prepend_block do
    def read_attribute(attr_name)
      super("modified_#{attr_name}")
    end
  end

  def read_attribute(attr_name)
    puts "Reading #{attr_name}"
  end
end

Example.new.read_attribute(:foo)
# Outputs: Reading modified_foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM