在 Rails 中运行另一个方法之前调用一个方法

发布于 2025-01-06 19:08:05 字数 289 浏览 2 评论 0原文

我有一个 Model,其中包含 method_1method_10。我还有ModelObserver。 我想在调用 method1 到 method_9 之前通知 ModelObserver,但不通知 method_10

有没有一种干燥的方式来编写这个,而不是在所有 9 个方法中重复 notify_observers(:after_something) ?

I have a Model, which has method_1 to method_10. I also have ModelObserver.
I would like to notifiy ModelObserver before invoking method1 to method_9, but not method_10.

Is there a DRY way to write this, instead of repeating notify_observers(:after_something) in all 9 methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2025-01-13 19:08:05

config/initializers 目录中添加一个名为 monkey_patches.rb 的文件。

class Object
  def self.method_hook(*args)
    options = args.extract_options!
    return unless (options[:before].present? or options[:after].present?)
    args.each do |method_name|      
      old_method = instance_method(method_name) rescue next

      define_method(method_name) do |*args|
        # invoke before callback
        if options[:before].present?
          options[:before].is_a?(Proc) ? options[:before].call(method_name, self):
            send(options[:before], method_name) 
        end

        # you can modify the code to call after callback 
        # only when the old method returns true etc..
        old_method.bind(self).call(*args)

        # invoke after callback
        if options[:after].present?
          options[:after].is_a?(Proc) ? options[:after].call(method_name, self): 
            send(options[:after], method_name) 
        end
      end
    end
  end
end  

该补丁使您能够在类的实例方法上添加 beforeafter 回调。钩子可以是:

  • 接受一个参数的实例方法的名称
  • 接受两个参数的 lambda

可以在同一个方法上注册多个钩子。被挂钩的方法应该出现在挂钩之前。

例如:

class Model < ActiveRecord::Base

  def method1
  end

  def method2
  end

  def method3
  end

  def method4
  end

  def update_cache
  end

  # instance method name as `after` callback parameter
  method_hook :method1, :method2, :after => :update_cache

  # lambda as `before` callback parameter
  method_hook :method1, :method2, 
    :before => lambda{|name, record| p name;p record}

  # lambda as `after` callback parameter
  method_hook :method3, :method4, 
    :after => lambda{|name, record| 
       Model2.increment_counter(:post_count, record.model2_id)}

end

Add a file called monkey_patches.rb in config/initializers dirctory.

class Object
  def self.method_hook(*args)
    options = args.extract_options!
    return unless (options[:before].present? or options[:after].present?)
    args.each do |method_name|      
      old_method = instance_method(method_name) rescue next

      define_method(method_name) do |*args|
        # invoke before callback
        if options[:before].present?
          options[:before].is_a?(Proc) ? options[:before].call(method_name, self):
            send(options[:before], method_name) 
        end

        # you can modify the code to call after callback 
        # only when the old method returns true etc..
        old_method.bind(self).call(*args)

        # invoke after callback
        if options[:after].present?
          options[:after].is_a?(Proc) ? options[:after].call(method_name, self): 
            send(options[:after], method_name) 
        end
      end
    end
  end
end  

The patch enables you to add before and after callbacks on an instance method of a class. A hook can be:

  • The name of an instance method which accepts one parameter
  • A lambda accepting two parameters

Multiple hooks can be registered on a same method. The method being hooked should come before the hook.

E.g:

class Model < ActiveRecord::Base

  def method1
  end

  def method2
  end

  def method3
  end

  def method4
  end

  def update_cache
  end

  # instance method name as `after` callback parameter
  method_hook :method1, :method2, :after => :update_cache

  # lambda as `before` callback parameter
  method_hook :method1, :method2, 
    :before => lambda{|name, record| p name;p record}

  # lambda as `after` callback parameter
  method_hook :method3, :method4, 
    :after => lambda{|name, record| 
       Model2.increment_counter(:post_count, record.model2_id)}

end
蓝梦月影 2025-01-13 19:08:05

像这样的事情怎么样?

def notify; puts "Was notified."; end
def method1; end
def method2; end
def method3; end

def original
  notify
  method1
  notify
  method2
  method3
end

def dry
  [:method1, :method2].each do |m|
    notify
    send(m)
  end
  method3
 end

original
dry

How about something like this?

def notify; puts "Was notified."; end
def method1; end
def method2; end
def method3; end

def original
  notify
  method1
  notify
  method2
  method3
end

def dry
  [:method1, :method2].each do |m|
    notify
    send(m)
  end
  method3
 end

original
dry
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文