Tag: mixins

包含测试辅助模块时的“未初始化常量”

在尝试将辅助模块包含到测试中时,我得到了一个未初始化的常量错误。 我的rails测试目录中有以下文件 functional> admin> school_controller_test.rb functional> controller_helper.rb 类/模块体如下: module ControllerHelper def check_sort_order (items, column, direction) … end end class Admin::SchoolsControllerTest < ActionController::TestCase include ::ControllerHelper test "should sort by columns" do check_sort_order(assigns(:schools), 'schools.name', 'asc') check_sort_order(assigns(:schools), 'schools.name', 'desc') end end 当我运行它时,测试输出是: /…/.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in`const_missing’:未初始化的常量ControllerHelper(NameError) ) 我已尝试使用命名空间,但无法将模块混合在一起! 任何想法为什么我收到此错误? 或者这是提取常见测试function的正确方法吗? 我对Rails很新,所以任何建议都会受到赞赏:) 干杯!

在类定义的末尾执行mixin方法

我有一个Mix-in反映在接收器类上以生成一些代码。 这意味着我需要在类定义的最后执行类方法,就像在这个简单的愚蠢的例子中一样: module PrintMethods module ClassMethods def print_methods puts instance_methods end end def self.included(receiver) receiver.extend ClassMethods end end class Tester include PrintMethods def method_that_needs_to_print end print_methods end 我想让mixin自动为我做这件事,但我无法想出办法。 我的第一个想法是将receiver.print_methods添加到self.included in mixin中,但这不起作用,因为我想要它反映的方法还没有被声明。 我可以在课程结束时调用include PrintMethods ,但感觉就像糟糕的forms。 是否有任何技巧可以实现这一点,所以我不需要在类定义的末尾调用print_methods ?

理解可比较的mixin和可枚举的mixin

我是一个新手,学习ruby。 希望能够更好地理解所提出的问题。 我不明白使用类似的mixin和可枚举的mixin。 我的意思是,当我们需要使用它们时,我们不会在课堂中包含这些,对吗? 如果我们想比较两个对象,我们只需写x> y。 那么明确使用它们有什么用?

在Ruby或Rails中,为什么有时在课堂上“包括”,有时候在课堂外?

我想 class ApplicationController < ActionController::Base include Foo 是添加“mixin” – 以便将Foo模块中的所有方法都视为ApplicationController的方法。 但现在我看到了代码 include Bar class ApplicationController < ActionController::Base include Foo 那为什么它在ApplicationController之外呢? 这与将它放在ApplicationController的更常见用法有什么不同?

有没有办法用Rspec存根包含模块的方法?

我有一个包含在另一个模块中的模块,它们都实现了相同的方法。 我想存根包含模块的方法,如下所示: module M def foo :M end end module A class << self include M def foo super end end end describe "trying to stub the included method" do before { allow(M).to receive(:foo).and_return(:bar) } it "should be stubbed when calling M" do expect(M.foo).to eq :bar end it "should be stubbed when calling A" do […]

在Mixins中初始化实例变量

是否有任何干净的方法来初始化模块中的实例变量以用作Mixin? 例如,我有以下内容: module Example def on(…) @handlers ||= {} # do something with @handlers end def all(…) @all_handlers ||= [] # do something with @all_handlers end def unhandled(…) @unhandled ||= [] # do something with unhandled end def do_something(..) @handlers ||= {} @unhandled ||= [] @all_handlers ||= [] # potentially do something with any of […]

“Ruby方式”(mixins和类重新开放)与dependency injection

在研究mixins与dependency injection时,我经常听到“Ruby方式”这个短语。 开发人员通常会说些什么 Ruby允许您重新打开类并重新定义方法意味着您可以在测试时轻松地将新引用“注入”代码中。 (见http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited#6 ) 但测试不是我主要考虑的问题; 我担心的是课堂重用。 我想要可以在多个企业级Rails应用程序中重用的类。 那么REUSING类会发生什么? 使用mixins和重新打开类似乎并没有提供一种方法来编写类,使它们与特定于应用程序的细节分离,而无需额外的工作。 但也许我错了。 如果我是,有人可以提供一个链接到包含示例代码的文章,该文章清楚地解释了如何使用mixins和重新打开类正确地完成此操作吗? 例如,这里的类Foo耦合到类Logger: class Foo def initialize @logger = new_logger end def new_logger Logger.new end end 是的,我可以重新打开Foo并重新定义new_logger,但我无法相信这被认为是编写可由多个Rails应用程序使用的可重用类的现实标准方法。

在几个文件中打破ruby模块

我有一个ruby模块,应该包含很多类 module A class First #somemethods end class Second #somemethods end class Third #somemethods end end 我想在rails中做的是将这些类拆分成几个文件,将这个庞大的模块拆分成几个相关文件的最佳做法是什么?

使用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 […]

使用基类和基本模块重构ActiveRecord模型

A类和B类相同: class A < ActiveRecord::Base def foo puts "foo" end end class B < ActiveRecord::Base def foo puts "foo" end end 像这样重构与基类之间有什么区别: class Base < ActiveRecord::Base def foo puts "foo" end end class A < Base end class B < Base end 与使用基本模块的情况相比 : module Base def foo puts “foo” end end class A < […]