Class: PluginList

Inherits:
Array
  • Object
show all
Defined in:
lib/loaded_plugins/plugin_list.rb

Overview

The PluginList class is an array, enhanced to allow access to loaded plugins by name, and iteration over loaded plugins in order of priority.

Each loaded plugin has a corresponding Plugin instance within this array, and the order the plugins were loaded is reflected in the entries in this array.

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Finds plugins with the given name or index. For example,

Rails.plugins[0]      # => The first plugin loaded
Rails.plugins[:foo]   # => The "foo" plugin
Rails.plugins['foo']  # => The "foo" plugin

This will return an instance of the Plugin class.



14
15
16
17
18
19
20
# File 'lib/loaded_plugins/plugin_list.rb', line 14

def [](name)
  if name.is_a?(String) || name.is_a?(Symbol)
    self.find {|plugin| plugin.name.to_s == name.to_s}
  else
    super
  end
end

#by_precedence(&block) ⇒ Object

Iterates through each plugin by priority. The last loaded plugin will always have the highest priority.

Effectively, this is like Rails.plugins.reverse. When given a block, it behaves like Rails.plugins.reverse.each.



46
47
48
49
50
51
52
# File 'lib/loaded_plugins/plugin_list.rb', line 46

def by_precedence(&block)
  if block_given?
    reverse.each { |x| yield x }
  else 
    reverse
  end
end

#find_by_names(names = nil) ⇒ Object

Gets the plugins in the specified comma-delimited list. If the list is not specified (i.e. nil), then all plugins are returned.

If a specific plugin cannot be found, an exception will be raised indicating so.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/loaded_plugins/plugin_list.rb', line 27

def find_by_names(names = nil)
  if names
    plugins = names.split(',')
    missing_plugins = plugins - map(&:name)
    
    raise ArgumentError, "Couldn't find plugin(s): #{missing_plugins.to_sentence}" if missing_plugins.any?
    plugins.collect! {|name| self[name]}
  else
    plugins = self
  end
  
  plugins
end