Sha256: 747b91038f61e955c4a20f542f9a6478a55e58aa5804673b87b159788443068a

Contents?: true

Size: 1.92 KB

Versions: 19

Compression:

Stored size: 1.92 KB

Contents

module RuGUI
  module Plugin
    class Loader
      attr_accessor :initializer
      attr_accessor :configurations
      cattr_accessor :located_plugins

      def initialize(initializer, configurations)
        self.initializer = initializer
        self.configurations = configurations
        @@located_plugins ||= []
      end

      def load_plugins
        plugins.each do |plugin|
          plugin.load unless plugin.loaded?
          register_as_loaded(plugin)
        end
      end

      def plugins
        @plugins ||= locate_plugins
      end

      protected
        # Locate all plugins in APPLICATION_ROOT/vendor/plugins
        def locate_plugins
          Dir.glob(File.join(APPLICATION_ROOT, "vendor", "plugins", "*")).each do |dir|
            @@located_plugins << Location.new(dir)
          end
          @@located_plugins
        end

        # Register plugins as loaded.
        def register_as_loaded(plugin)
          plugin.loaded = true
        end
    end

    # This class is a representation of RuGUI plugins.
    class Location
      attr_accessor :plugin_name
      attr_accessor :dir
      attr_accessor :loaded

      include RuGUI::LogSupport

      def initialize(dir)
        self.dir = dir
        self.plugin_name = dir.split(File::SEPARATOR).last
      end

      # Load plugins.
      def load
        $LOAD_PATH.unshift(File.join(self.dir, "lib")) if File.directory?(self.dir)
        $LOAD_PATH.uniq!

        init_file = File.expand_path(File.join(self.dir, "init.rb"))
        if File.exist?(init_file)
          require_for init_file
        else
          logger.warn "The init file for (#{self.plugin_name}) was not found."
        end
      end

      def require_for(init_file)
        require init_file
      rescue Exception
        logger.error "An error occurred while loading #{self.plugin_name}. Checks its init file: #{$!}"
      end

      def loaded?
        self.loaded ||= false
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
intelitiva-rugui-1.2.0 lib/rugui/plugin/loader.rb
intelitiva-rugui-1.3.0 lib/rugui/plugin/loader.rb
intelitiva-rugui-1.3.1 lib/rugui/plugin/loader.rb
intelitiva-rugui-1.3.2 lib/rugui/plugin/loader.rb
rugui-1.6.0 lib/rugui/plugin/loader.rb
rugui-1.5.4 lib/rugui/plugin/loader.rb
rugui-1.5.3 lib/rugui/plugin/loader.rb
rugui-1.5.2 lib/rugui/plugin/loader.rb
rugui-1.5.0 lib/rugui/plugin/loader.rb
rugui-1.4.3 lib/rugui/plugin/loader.rb
rugui-1.4.2 lib/rugui/plugin/loader.rb
rugui-1.4.0 lib/rugui/plugin/loader.rb
rugui-1.3.3 lib/rugui/plugin/loader.rb
rugui-1.3.1 lib/rugui/plugin/loader.rb
rugui-1.3.2 lib/rugui/plugin/loader.rb
rugui-1.2.0 lib/rugui/plugin/loader.rb
rugui-1.2.1 lib/rugui/plugin/loader.rb
rugui-1.3.0 lib/rugui/plugin/loader.rb
rugui-1.2.2 lib/rugui/plugin/loader.rb