Sha256: ab64018ce97535de61a35bc8425a62480fe6066589a1a007dae13c5d756875d3

Contents?: true

Size: 1.23 KB

Versions: 15

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Esse
  class Index
    module ClassMethods
      attr_reader :plugins

      def plugin(plugin, **kwargs, &block)
        mod = plugin.is_a?(Module) ? plugin : load_plugin_module(plugin)

        unless @plugins.include?(mod)
          @plugins << mod
          mod.apply(self, **kwargs, &block) if mod.respond_to?(:apply)
          extend(mod::IndexClassMethods) if mod.const_defined?(:IndexClassMethods, false)
          if mod.const_defined?(:RepositoryClassMethods, false)
            repo_hash.each_value.each { |repo| repository_plugin_extend(repo, mod::RepositoryClassMethods) }
          end
        end

        mod.configure(self, **kwargs, &block) if mod.respond_to?(:configure)
      end

      private

      def repository_plugin_extend(repo_class, mod)
        return if repo_class.singleton_class.included_modules.include?(mod)

        repo_class.extend(mod)
      end

      def load_plugin_module(name)
        module_name = Hstring.new(name)
        unless Esse::Plugins.const_defined?(module_name.camelize.to_s, false)
          require "esse/plugins/#{module_name.underscore}"
        end
        Esse::Plugins.const_get(module_name.camelize.to_s)
      end
    end

    extend ClassMethods
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/index/plugins.rb
esse-0.4.0.rc3 lib/esse/index/plugins.rb
esse-0.4.0.rc2 lib/esse/index/plugins.rb
esse-0.4.0.rc1 lib/esse/index/plugins.rb
esse-0.3.5 lib/esse/index/plugins.rb
esse-0.3.4 lib/esse/index/plugins.rb
esse-0.3.3 lib/esse/index/plugins.rb
esse-0.3.2 lib/esse/index/plugins.rb
esse-0.3.1 lib/esse/index/plugins.rb
esse-0.3.0 lib/esse/index/plugins.rb
esse-0.2.6 lib/esse/index/plugins.rb
esse-0.2.5 lib/esse/index/plugins.rb
esse-0.2.4 lib/esse/index/plugins.rb
esse-0.2.3 lib/esse/index/plugins.rb
esse-0.2.2 lib/esse/index/plugins.rb