class SmartCore::Initializer::Plugins::Registry

@api private @since 0.1.0

Attributes

access_lock[R]

@return [Mutex]

@api private @since 0.1.0

plugin_set[R]

@return [Hash]

@api private @since 0.1.0

Public Class Methods

new() click to toggle source

@return [void]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 13
def initialize
  @plugin_set = {}
  @access_lock = Mutex.new
end

Public Instance Methods

[](plugin_name) click to toggle source

@param plugin_name [Symbol, String] @return [SmartCore::Initializer::Plugins::Abstract]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 23
def [](plugin_name)
  thread_safe { fetch(plugin_name) }
end
[]=(plugin_name, plugin_module)
Alias for: register
each(&block) click to toggle source

@param block [Block] @return [Enumerable]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 59
def each(&block)
  thread_safe { iterate(&block) }
end
loaded() click to toggle source

@return [Hash<String,Class<SmartCore::Initializer::Plugins::Abstract>>]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 50
def loaded
  thread_safe { loaded_plugins }
end
names() click to toggle source

@return [Array<String>]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 42
def names
  thread_safe { plugin_names }
end
register(plugin_name, plugin_module) click to toggle source

@param plugin_name [Symbol, String] @param plugin_module [SmartCore::Initializer::Plugins::Abstract] @return [void]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 33
def register(plugin_name, plugin_module)
  thread_safe { apply(plugin_name, plugin_module) }
end
Also aliased as: []=

Private Instance Methods

apply(plugin_name, plugin_module) click to toggle source

@param plugin_name [Symbol, String] @param plugin_module [SmartCore::Initializer::Plugins::Abstract] @return [void]

@raise [SmartCore::Initializer::AlreadyRegisteredPluginError]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 127
  def apply(plugin_name, plugin_module)
    plugin_name = indifferently_accessible_plugin_name(plugin_name)

    if registered?(plugin_name)
      raise(SmartCore::Initializer::AlreadyRegisteredPluginError, <<~ERROR_MESSAGE)
        #{plugin_name} plugin already exists
      ERROR_MESSAGE
    end

    plugin_set[plugin_name] = plugin_module
  end
fetch(plugin_name) click to toggle source

@param plugin_name [Symbol, String] @return [SmartCore::Initializer::Plugins::Abstract]

@raise [SmartCore::Initializer::UnregisteredPluginError]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 146
  def fetch(plugin_name)
    plugin_name = indifferently_accessible_plugin_name(plugin_name)

    unless registered?(plugin_name)
      raise(SmartCore::Initializer::UnregisteredPluginError, <<~ERROR_MESSAGE)
        #{plugin_name} plugin is not registered
      ERROR_MESSAGE
    end

    plugin_set[plugin_name]
  end
indifferently_accessible_plugin_name(plugin_name) click to toggle source

@param key [Symbol, String] @return [String]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 163
def indifferently_accessible_plugin_name(plugin_name)
  plugin_name.to_s
end
iterate(&block) click to toggle source

@param block [Block] @return [Enumerable]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 98
def iterate(&block)
  block_given? ? plugin_set.each_pair(&block) : plugin_set.each_pair
end
loaded_plugins() click to toggle source

@return [Array<SmartCore::Initializer::Plugins::Abstract>]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 115
def loaded_plugins
  plugin_set.select { |_plugin_name, plugin_module| plugin_module.loaded? }
end
plugin_names() click to toggle source

@return [Array<String>]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 89
def plugin_names
  plugin_set.keys
end
registered?(plugin_name) click to toggle source

@param plugin_name [String] @return [Boolean]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 107
def registered?(plugin_name)
  plugin_set.key?(plugin_name)
end
thread_safe() { || ... } click to toggle source

@return [void]

@api private @since 0.1.0

# File lib/smart_core/initializer/plugins/registry.rb, line 81
def thread_safe
  access_lock.synchronize { yield if block_given? }
end