简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM