【发布时间】:2011-04-30 23:20:23
【问题描述】:
我看到一些地方在 gems 中引用了 *_without_* 方法,但我看不到它们是在哪里定义的。以下是来自 validationgroup 和 delayed_jobs gem 的两个示例。
-
在validation_group.rb 中,定义了一个方法
add_with_validation_group,在其最后一行引用add_without_validation_group;但是,add_without_validation_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 的message_sending.rb 中,动态传递的方法参数被称为
#{method}_without_send_later。但是,我只看到#{method}_with_send_later在此 gem 中的任何位置定义。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 魔法。但是,我似乎无法弄清楚在 Google 中搜索什么来自己回答这个问题。
【问题讨论】:
标签: ruby-on-rails gem rubygems metaprogramming