开发者

Include plugin in module in Rails

This is a definite newb question:

I have a module defined in my lib/ directory that I call from a sweeper and as a rake task. Inside the module I want to reference my spawn plugin. Just including methods from that plugin doesn't work (undefined method error), nor does any version of require or include that I've tried开发者_运维百科. What do I need to do to include it?

Thank you!

EDIT:

Here is my latest effort:

require 'vendor/plugins/spawn/init.rb'

module MyModule

  include Spawn

  def self.my_method

     spawn(:method => :thread, :nice => 9) do # also tried Spawn::spawn

         ...

     end
  end
end

The error I get is:

undefined method 'spawn' for MyModule:Module #spawn or undefined method 'spawn' for Spawn:Module # Spawn::spawn


The including has to be done from within the plugin. When you "reopen" a module (which is in the lib/ dir), remember to require the original file before making changes to it (the module).

EDIT: Considering that the plugin is third party, you could try:

Spawn.module_eval do
  module_function :spawn
  public :spawn
end

module MyModule
  def self.my_method
    Spawn.spawn(...)
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜