Tag: metaprogramming

在Rails 5 Application Record类中包含一个模块

我正在使用Application Record来简化整个应用程序中的共享逻辑。 这是一个为布尔值及其逆写入范围的示例。 这很好用: class ApplicationRecord { where(“#{attr}”: true) }) scope(opposite, -> { where(“#{attr}”: false) }) if opposite.present? end end class User < ApplicationRecord boolean_scope :verified, :unverified end class Message < ApplicationRecord boolean_scope :sent, :pending end 我的应用程序记录类得到了足够长的时间,我将它分解为单个模块并根据需要加载它们是有意义的。 这是我尝试的解决方案: class ApplicationRecord { where(“#{attr}”: true) }) scope(opposite, -> { where(“#{attr}”: false) }) if opposite.present? end end class […]

使用method_added了解ruby元编程以动态覆盖实例方法

我有以下来自Programming Ruby 1.9的代码(略微适应)我只是想确保我的思维过程是准确的 module Trace def self.included(culprit) #Inject existing methods with tracing code: culprit.instance_methods(false).each do |func| inject(culprit, func) end #Override the singletons method_added to ensure all future methods are injected. def culprit.method_added(meth) unless @trace_calls_internal @trace_calls_internal = true Trace.inject(self, meth) #This will call method_added itself, the condition prevents infinite recursion. @trace_calls_internal = false end end end […]

从外部gem扩充模型

我在我们的网站上使用refinerycms让技术水平较低的员工更新内容。 在gem中,它们有一个Page类,用于映射站点上的每个顶级页面。 我想在这个Page类上使用acts_as_taggable gem。 现在我可以将acts_as_taggle声明直接添加到page.rb文件中,但是我必须维护一个单独的git repo来跟踪我的版本和官方发行版之间的差异。 基于SO的其他一些问题,我创建了一个初始化器和扩展器,如下所示: LIB / page_extensions.rb: module Pants module Extensions module Page module ClassMethods def add_taggable acts_as_taggable end end def self.included(base) base.extend(ClassMethods).add_taggable end end end end 配置/初始化/ pants.rb require ‘page_extensions’ Page.send :include, Pants::Extensions::Page 应用程序/视图/布局/ application.html.erb … Tags: 我第一次从服务器请求页面时,它正确输出页面上的所有标签。 但是,如果我点击刷新I而不是获得NoMethodError表明tag_list未定义。 我是rails的新手,所以也许我的假设是错误的,但我希望对Page.send的调用会对Page类进行永久性更改,而不是对类的特定实例进行永久性更改。 那么如何在每个请求中将acts_as_taggable添加到Page类?

如何在运行时动态创建实例方法?

[ruby1.8] 假设我有: dummy “string” do puts “thing” end 现在,这是对一个方法的调用,该方法具有一个字符串和一个块作为输入参数。 尼斯。 现在假设我可以有很多类似的调用(不同的方法名称,相同的参数)。 例: otherdummy “string” do puts “thing” end 现在因为他们做同样的事情,而且他们可以成百上千,我不想为想要的类中的每一个创建一个实例方法。 我想找到一种基于一般规则在运行时动态定义方法的智能方法。 那可能吗? 通常使用哪种技术? 谢谢

当前的Ruby方法是通过super调用的吗?

在运行时的方法中,有没有办法知道该方法是否已通过子类中的super调用? 例如 module SuperDetector def via_super? # what goes here? end end class Foo include SuperDetector def bar via_super? ? ‘super!’ : ‘nothing special’ end end class Fu “nothing special” Fu.new.bar # => “super!” 我怎么能写via_super? ,或者,如有必要, via_super?(:bar) ?

获取包含模块的类列表

我有一个mixin,我想获得包含它的所有类的列表。 在mixin模块中,我做了以下事情: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def self.classes @classes end end class MyClass include MyModule end 这非常有效: > MyModule.classes #=> nil > MyClass.new #=> # > MyModule.classes #=> [“MyClass”] 现在,我想将这部分提取到一个单独的模块中,该模块可以包含在我的其他mixins中。 所以,我想出了以下内容: module ListIncludedClasses def self.included(base) p “…adding #{base.name} to #{self.name}.classes” @classes ||= [] @classes << base.name base.extend(ClassMethods) end […]