# encoding: utf-8 require 'fedux_org_stdlib/require_files' require 'fedux_org_stdlib/core_ext/array/list' require_library %w(active_support/core_ext/object/blank) module FeduxOrgStdlib module GemPlugins # Plugin class Plugin include Comparable private attr_accessor :enabled, :active attr_writer :gem_name attr_reader :prefix public attr_reader :name, :gem_name, :required_file def initialize(name, gem_name:, enabled:, prefix: nil) @name = name @gem_name = gem_name @enabled = enabled @prefix = prefix end # Disable a plugin. (prevents plugin from being loaded, cannot # disable an already activated plugin) def disable self.enabled = false end # Enable a plugin. (does not load it immediately but puts on # 'white list' to be loaded) def enable self.enabled = true end # Activate the plugin (require the gem - enables/loads the # plugin immediately at point of call, even if plugin is # disabled) # Does not reload plugin if it's already active. def activate variants = [] variants << gem_name variants << gem_name.gsub(/-/, '/') variants << prefix.source.sub(/-$/, '') if prefix begin unless active? begin required_file = variants.shift require required_file @required_file = required_file rescue LoadError retry unless variants.blank? raise LoadError end end rescue LoadError => e warn "Found plugin #{gem_name}, but could not require '#{variants.to_list(last_separator: ' or ')}'" warn e rescue StandardError => e warn "require '#{gem_name}' # Failed, saying: #{e}" end self.active = true self.enabled = true end # Is plugin active? def active? active == true end # Is plugin enabled? def enabled? enabled == true end # Is an existing plugin def blank? false end # @private def <=>(other) name <=> other.name end end end end