Ruby on rails 这些在哪里;没有「;如何定义Rails gems中的方法?

Ruby on rails 这些在哪里;没有「;如何定义Rails gems中的方法?,ruby-on-rails,gem,rubygems,metaprogramming,Ruby On Rails,Gem,Rubygems,Metaprogramming,我见过一些地方在gems中引用了*\u而没有*方法,但我看不到它们是在哪里定义的。下面是来自validationgroup和delayed_作业gems的两个示例 在中,有一种方法add_with_validation_group,定义该方法在其最后一行引用add_without_validation_group;但是,add\u without\u validation\u group似乎没有在任何地方定义 def add_with_validation_group(attribute,

我见过一些地方在gems中引用了
*\u而没有*
方法,但我看不到它们是在哪里定义的。下面是来自
validationgroup
delayed_作业
gems的两个示例

  • 在中,有一种方法
    add_with_validation_group
    ,定义该方法在其最后一行引用
    add_without_validation_group
    ;但是,
    add\u without\u validation\u group
    似乎没有在任何地方定义

    def add_with_validation_group(attribute,
                                  msg = @@default_error_messages[:invalid], *args,
                                  &block)
      add_error = true
      if @base.validation_group_enabled?
        current_group = @base.current_validation_group
        found = ValidationGroup::Util.current_and_ancestors(@base.class).
          find do |klass|
            klasses = klass.validation_group_classes
            klasses[klass] && klasses[klass][current_group] &&
            klasses[klass][current_group].include?(attribute)
          end
        add_error = false unless found
      end
      add_without_validation_group(attribute, msg, *args,&block) if add_error
    end
    
  • 在DelayedJob中,动态传递的方法参数被称为
    {method}\u,以后不发送。然而,我只看到在这个gem中的任何地方定义了
    {method}\u和后面的

    def handle_asynchronously(method)
      without_name = "#{method}_without_send_later"
      define_method("#{method}_with_send_later") do |*args|
        send_later(without_name, *args)
      end
      alias_method_chain method, :send_later
    end
    

  • 因此,我相信这些“无”方法中缺少了一些Rails魔法。然而,我似乎不知道在谷歌上搜索什么来回答我自己的问题。

    这就是
    alias\u method\u chain
    提供给你的

    基本上当你说

    alias_method_chain :some_method, :feature
    
    我们为您提供了两种方法:

    some_method_with_feature
    some_method_without_feature
    
    发生的情况是,当调用原始的
    some\u方法
    时,它实际上调用了
    some\u方法
    。然后,您将获得一个对不带\u功能的
    某个\u方法\u的引用
    ,这是您最初的方法声明(即用于回退/默认行为)。因此,您需要定义
    some\u method\u和\u feature
    来实际执行任务,正如我所说,当您调用
    some\u method

    例如:

    def do_something
      "Do Something!!"
    end
    
    def do_something_with_upcase
      do_something_without_upcase.upcase
    end
    
    alias_method_chain :do_something, :upcase
    
    do_something # => "DO SOMETHING!!"
    

    请参阅文档:

    这就是
    alias\u method\u chain
    为您提供的内容

    基本上当你说

    alias_method_chain :some_method, :feature
    
    我们为您提供了两种方法:

    some_method_with_feature
    some_method_without_feature
    
    发生的情况是,当调用原始的
    some\u方法
    时,它实际上调用了
    some\u方法
    。然后,您将获得一个对不带\u功能的
    某个\u方法\u的引用
    ,这是您最初的方法声明(即用于回退/默认行为)。因此,您需要定义
    some\u method\u和\u feature
    来实际执行任务,正如我所说,当您调用
    some\u method

    例如:

    def do_something
      "Do Something!!"
    end
    
    def do_something_with_upcase
      do_something_without_upcase.upcase
    end
    
    alias_method_chain :do_something, :upcase
    
    do_something # => "DO SOMETHING!!"
    
    请参阅文档: