Sha256: d96259826f71427309e01bc9df205e0e2a1047d469b407c50e3515e3b3a95ecd

Contents?: true

Size: 1.51 KB

Versions: 20

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require_relative './manager.rb'
require_relative '../config.rb'

module Fusuma
  module Plugin
    # Create a Plugin Class with extending this class
    class Base
      # when inherited from subclass
      def self.inherited(subclass)
        subclass_path = caller_locations(1..1).first.path
        Manager.add(plugin_class: subclass, plugin_path: subclass_path)
      end

      # get inherited classes
      # @example
      #  [Vectors::Vector]
      # @return [Array]
      def self.plugins
        Manager.plugins[name]
      end

      # config parameter name and Type of the value of parameter
      # @return [Hash]
      def config_param_types
        raise NotImplementedError, "override #{self.class.name}##{__method__}"
      end

      # @return [Object]
      def config_params(key = nil, base: config_index)
        params = Config.search(base) || {}

        return params unless key

        params.fetch(key, nil).tap do |val|
          next if val.nil?

          # NOTE: Type checking for config.yml
          param_types = Array(config_param_types.fetch(key))

          next if param_types.any? { |klass| val.is_a?(klass) }

          MultiLogger.error('Please fix config.yml.')
          MultiLogger.error(":#{base.keys.map(&:symbol)
            .join(' => :')} => :#{key} should be #{param_types.join(' OR ')}.")
          exit 1
        end
      end

      def config_index
        Config::Index.new(self.class.name.gsub('Fusuma::', '').underscore.split('/'))
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
fusuma-2.0.0.pre lib/fusuma/plugin/base.rb
fusuma-1.11.1 lib/fusuma/plugin/base.rb
fusuma-1.10.2 lib/fusuma/plugin/base.rb
fusuma-1.10.1 lib/fusuma/plugin/base.rb
fusuma-1.10.0 lib/fusuma/plugin/base.rb
fusuma-1.9.0 lib/fusuma/plugin/base.rb
fusuma-1.8.0 lib/fusuma/plugin/base.rb
fusuma-1.7.0 lib/fusuma/plugin/base.rb
fusuma-1.6.0 lib/fusuma/plugin/base.rb
fusuma-1.5.0 lib/fusuma/plugin/base.rb
fusuma-1.4.1 lib/fusuma/plugin/base.rb
fusuma-1.4.0 lib/fusuma/plugin/base.rb
fusuma-1.3.3 lib/fusuma/plugin/base.rb
fusuma-1.3.2 lib/fusuma/plugin/base.rb
fusuma-1.3.1 lib/fusuma/plugin/base.rb
fusuma-1.3.0 lib/fusuma/plugin/base.rb
fusuma-1.2.1 lib/fusuma/plugin/base.rb
fusuma-1.2 lib/fusuma/plugin/base.rb
fusuma-1.1 lib/fusuma/plugin/base.rb
fusuma-1.0 lib/fusuma/plugin/base.rb