1

I want to create a base lass and then load all sub classes (plugins) and process all of them via each loop. This is working example:

require 'set'

class Plugin
  # Keep the plugin list inside a set so we don't double-load plugins
  @plugins = Set.new

  def self.plugins
    @plugins
  end

  def self.register_plugins
    # Iterate over each symbol in the object space
    Object.constants.each do |klass|
      # Get the constant from the Kernel using the symbol
      const = Kernel.const_get(klass)
      # Check if the plugin has a super class and if the type is Plugin
      if const.respond_to?(:superclass) and const.superclass == Plugin
        @plugins << const
      end
    end
  end
end

class DogPlugin < Plugin

  def self.handle_command(cmd)
    p "Command received #{cmd}"
  end

end
class CatPlugin < Plugin

  def self.handle_command(cmd)
    p "Command received #{cmd}"
  end

end

Plugin.register_plugins

# Test that we can send a message to each plugin
Plugin.plugins.each do |plugin|
  plugin.handle_command('test')
end

This code sample works perfectly. Output is:

"Command received test"
"Command received test"
=> #<Set: {DogPlugin, CatPlugin}>

However, In my rails application, my custom implementations are in modules. Lets say I have an A module. In this case it doesn't work.

require 'set'

module A
  class Plugin
    @plugins = Set.new

    class << self
      attr_reader :plugins
    end

    def self.register_plugins
      # Iterate over each symbol in the object space
      Object.constants.each do |klass|
        # Get the constant from the Kernel using the symbol
        const = Kernel.const_get(klass)
        # Check if the plugin has a super class and if the type is Plugin
        if const.respond_to?(:superclass) && (const.superclass == A::Plugin)
          @plugins << const
        end
      end
    end
  end
end

module A
  class MyAction < Plugin
    def self.handle_command(cmd)
      puts "Command received #{cmd}"
    end
   end
end

A::Plugin.register_plugins
A::Plugin.plugins.each do |plugin|
  plugin.handle_command('test')
end

Set is empty and nothing get executed. Why?

See live sample here: https://repl.it/repls/EllipticalDamagedCategory

There are other types of plugin samples on the net but they have to initialized one-by-one. This sample code loads all plugins and execute same method in all of them. I need this functionality with modules.

4
  • It's probably an issue with load order. What we have done at my company is make a file in config/initializers which loads the files in the order they're needed. Apr 3, 2019 at 18:01
  • At my sample, If I change order, it fails. I think sample is in good order. It has something to do with modules. Because when same example made without module A with same order, It works.
    – Dennis
    Apr 3, 2019 at 18:05
  • You know that Class#inherited is a thing correct? Just seems like a better way to implement than to use a loop. Updated Example using inherited Apr 3, 2019 at 20:56
  • Ah. I see. I will use this. Thanks.
    – Dennis
    Apr 10, 2019 at 9:13

1 Answer 1

1
require 'set'

module A
  class Plugin
    @plugins = Set.new

    def self.plugins
      @plugins
    end

    def self.register_plugins
      # Iterate over each symbol in the object space
      ::A.constants.each do |klass|
        # Get the constant from the Kernel using the symbol
        const = A.const_get(klass)
        puts const
        # Check if the plugin has a super class and if the type is Plugin

        if const.respond_to?(:superclass) && (const.superclass == Plugin)
          @plugins << const
        end
      end
    end
  end
end

module A
  class MyAction < Plugin
    def self.handle_command(cmd)
      puts "Command received #{cmd}"
    end
   end
end

A::Plugin.register_plugins
A::Plugin.plugins.each do |plugin|
  plugin.handle_command('test')
end
4
  • I am very sorry to say this but i quite didn't understand that. Could you please post a sample based on above to wake me up? :/
    – Dennis
    Apr 3, 2019 at 18:23
  • I updated with a working version, you need to load constants of the module itself.. Apr 3, 2019 at 18:40
  • Awesome. Thank you so much!
    – Dennis
    Apr 3, 2019 at 18:46
  • May I ask a quick question. I didn't want to open a new issue for this. When using standalone ruby it works, however, in Rails i think autoload feature breaks it. Even I explicitly require it, MyAction (my_action.rb) class does not found. Do you know anything about this? What might be the problem? (When I move MyAction to the same file with Plugin, it works).
    – Dennis
    Apr 3, 2019 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.