pluginator 0.11.3 → 0.11.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6454fa655abb05e55461de618af2de74dcdecf1
4
- data.tar.gz: eae4963322e3dd0ea9892c1965c5384131f728b8
3
+ metadata.gz: a42be419608abc90291c8358a4f23b1e43165cec
4
+ data.tar.gz: baa6ea7161c799f1455cc31fff04f4be8e594794
5
5
  SHA512:
6
- metadata.gz: 5a7e5992fb6a1203cb9a45681e8719b2d7a9836ffafeb1312b866275c1b9973d95bbdbba4ecd83ba8e4ec9d6ab0f6d83698dc39ece10dffd7566b73f5be783f8
7
- data.tar.gz: 0f19687c93b7ba4b14ac848308bdbd5eb4ee15903b29333c6f1880e845d742cde3a7a53090380596ca2121b8a267907a7386ac22c07113774e019c2b07692979
6
+ metadata.gz: 8cf984f2cb328242a966ae4cd58b4e19d2314689c00bac2a08356149ef8ea98bea0ef4864f39af033f79f4031b573c59b6b97dd0199280c320bf6c25c2441414
7
+ data.tar.gz: eea32d7a0e76329967ff1a9ef62d9cf3b92fc40433997148ccb5408c77d4f7956d829d3141daacd4428ca88d7a1640a2b5dc0046366fe01a9fd20d09ae54fb4a
data/README.md CHANGED
@@ -63,6 +63,16 @@ plugins.first_ask( "<type>", "method_to_call", *params) => plugin or nil
63
63
  plugins.first_ask!("<type>", "method_to_call", *params) => plugin or exception PluginatorError
64
64
  ```
65
65
 
66
+ ### First class
67
+
68
+ Find first plugin that clas matches the given name.
69
+
70
+ ```ruby
71
+ plugins = Pluginator.find("<group>", extends: %i{first_class})
72
+ plugins.first_class( "<type>", "<name>") => plugin or nil
73
+ plugins.first_class!("<type>", "<name>") => plugin or exception PluginatorError
74
+ ```
75
+
66
76
  ### Matching
67
77
 
68
78
  Map array of names to available plugins.
@@ -73,6 +83,12 @@ plugins.matching( "<type>", [<array_of_names>]) => [plugins] # nil for missing o
73
83
  plugins.matching!("<type>", [<array_of_names>]) => [plugins] or exception PluginatorError
74
84
  ```
75
85
 
86
+ ## Exceptions
87
+
88
+ - `PluginatorError` - base error for all Pluginator errors
89
+ - `MissingPlugin` - raised when plugin can not be found, generated by `*!` methods
90
+ - `MissingType` - raised when type can not be found, generated by `*!` methods
91
+
76
92
  ## Examples
77
93
 
78
94
  ### Example 1 - task plugins
data/lib/pluginator.rb CHANGED
@@ -2,6 +2,12 @@ require_relative "pluginator/extendable_autodetect"
2
2
  require_relative "pluginator/version"
3
3
 
4
4
  module Pluginator
5
+ # Find plugins for the given group
6
+ #
7
+ # @param group [String] name of plugins group
8
+ # @param type [String] optional name of type to load
9
+ # @param extends optional list of extension to extend into pluginator instance
10
+ # @return instance of Pluginator
5
11
  def self.group(group, type: nil, extends: [])
6
12
  Pluginator::ExtendableAutodetect.new(group, type: type, extends: extends)
7
13
  end
@@ -5,6 +5,10 @@ require_relative "name_converter"
5
5
  module Pluginator
6
6
  class Autodetect < Group
7
7
 
8
+ # Automatically load plugins for given group (and type)
9
+ #
10
+ # @param group [String] name of the plugins group
11
+ # @param type [String] optional name of the plugin type
8
12
  def initialize(group, type: nil)
9
13
  super(group)
10
14
  setup_autodetect(type)
@@ -1,4 +1,6 @@
1
1
  module Pluginator
2
+
3
+ # base error for all Pluginator errors
2
4
  class PluginatorError < RuntimeError
3
5
  private
4
6
  def list_to_s(list)
@@ -6,13 +8,20 @@ module Pluginator
6
8
  end
7
9
  end
8
10
 
11
+ # raised when plugin can not be found, generated by `*!` methods
9
12
  class MissingPlugin < PluginatorError
13
+ # @param type [String] type of the loaded plugin
14
+ # @param name [String] name of the loaded plugin
15
+ # @param list [Array] list of available plugins
10
16
  def initialize(type, name, list)
11
17
  super("Can not find plugin '#{name}' in #{list_to_s(list)} for type '#{type}'.")
12
18
  end
13
19
  end
14
20
 
21
+ # raised when type can not be found, generated by `*!` methods
15
22
  class MissingType < PluginatorError
23
+ # @param type [String] type of the loaded plugin
24
+ # @param list [Array] list of available types
16
25
  def initialize(type, list)
17
26
  super("Can not find type '#{type}' in #{list_to_s(list)}.")
18
27
  end
@@ -3,11 +3,20 @@ require_relative "autodetect"
3
3
  module Pluginator
4
4
  class ExtendableAutodetect < Autodetect
5
5
 
6
+ # Automatically load plugins for given group (and type)
7
+ # Extend instance with extensions if given.
8
+ #
9
+ # @param group [String] name of the plugins group
10
+ # @param type [String] optional name of the plugin type
11
+ # @param extends optional list of extension to extend into pluginator instance
6
12
  def initialize(group, type: nil, extends: [])
7
13
  super(group, type: type)
8
14
  extend_plugins(extends)
9
15
  end
10
16
 
17
+ # Extend pluginator instance with given extensions
18
+ #
19
+ # @param extends list of extension to extend into pluginator instance
11
20
  def extend_plugins(extends)
12
21
  extensions_matching(extends).each do |plugin|
13
22
  extend plugin
@@ -1,19 +1,28 @@
1
1
  module Pluginator
2
2
  class Group
3
+ # Group name used for plugins
3
4
  attr_reader :group
4
5
 
6
+ # @param group [String] name of the plugins group
5
7
  def initialize(group)
6
8
  setup_group(group)
7
9
  end
8
10
 
11
+ # @param [String] type of plugins to select
12
+ # @return [Array] list of plugins for type
9
13
  def [](type)
10
14
  @plugins[type.to_s]
11
15
  end
12
16
 
17
+ # @return [Array] list of plugin types loaded
13
18
  def types
14
19
  @plugins.keys
15
20
  end
16
21
 
22
+ # Register a new plugin, can be used to load custom plugins
23
+ #
24
+ # @param type [String] type for the klass
25
+ # @param klass [Class] klass of the plugin to add
17
26
  def register_plugin(type, klass)
18
27
  type = type.to_s
19
28
  @plugins[type] ||= []
@@ -1,3 +1,4 @@
1
1
  module Pluginator
2
- VERSION = "0.11.3"
2
+ # Version of Pluginator
3
+ VERSION = "0.11.4"
3
4
  end
@@ -1,10 +1,15 @@
1
1
  module Pluginator::Extensions
2
2
  module Conversions
3
+
4
+ # converts class name to a file name
3
5
  def class2string( klass )
4
6
  klass.to_s.gsub(/([A-Z])/m){|match| "_#{$1.downcase}" }[1..-1]
5
7
  end
8
+
9
+ # converts file name to a class name
6
10
  def string2class( str )
7
11
  str.to_s.capitalize.gsub(/_(.)/){ $1.upcase }
8
12
  end
13
+
9
14
  end
10
15
  end
@@ -4,13 +4,21 @@ module Pluginator::Extensions
4
4
  module FirstAsk
5
5
  include PluginsMap
6
6
 
7
+ # Call a method on plugin and return first one that returns `true`.
8
+ #
9
+ # @param type [String] name of type to search for plugins
10
+ # @param method_name [Symbol] name of the method to execute
11
+ # @param *params [Array] params to pass to the called method
12
+ # @return The first plugin that method call returns true
7
13
  def first_ask(type, method_name, *params)
8
14
  @plugins[type] or return nil
9
15
  @plugins[type].detect do |plugin|
10
- plugin.public_send(method_name, *params)
16
+ plugin.public_send(method_name.to_sym, *params)
11
17
  end
12
18
  end
13
19
 
20
+ # Call a method on plugin and return first one that returns `true`.
21
+ # Behaves like `first_ask` but throws exceptions if can not find anything.
14
22
  def first_ask!(type, method_name, *params)
15
23
  @plugins[type] or raise Pluginator::MissingType.new(type, @plugins.keys)
16
24
  @plugins[type].detect do |plugin|
@@ -6,10 +6,17 @@ module Pluginator::Extensions
6
6
  include PluginsMap
7
7
  include Conversions
8
8
 
9
+ # Find first plugin that clas matches the given name.
10
+ #
11
+ # @param type [String] name of type to search for plugins
12
+ # @param klass [Symbol or String] name of the searched class
13
+ # @return The first plugin that matches the klass
9
14
  def first_class(type, klass)
10
15
  (plugins_map(type) || {})[string2class(klass)]
11
16
  end
12
17
 
18
+ # Find first plugin that clas matches the given name.
19
+ # Behaves like `first_class` but throws exceptions if can not find anything.
13
20
  def first_class!(type, klass)
14
21
  @plugins[type] or raise Pluginator::MissingType.new(type, @plugins.keys)
15
22
  klass = string2class(klass)
@@ -6,12 +6,19 @@ module Pluginator::Extensions
6
6
  include PluginsMap
7
7
  include Conversions
8
8
 
9
+ # Map array of names to available plugins.
10
+ #
11
+ # @param type [String] name of type to search for plugins
12
+ # @param list [Array] list of plugin names to load
13
+ # @return [Array] list of loaded plugins
9
14
  def matching(type, list)
10
15
  list.map do |plugin|
11
16
  (plugins_map(type) || {})[string2class(plugin)]
12
17
  end
13
18
  end
14
19
 
20
+ # Map array of names to available plugins.
21
+ # Behaves like `matching` but throws exceptions if can not find anything.
15
22
  def matching!(type, list)
16
23
  @plugins[type] or raise Pluginator::MissingType.new(type, @plugins.keys)
17
24
  list.map do |plugin|
@@ -1,5 +1,9 @@
1
1
  module Pluginator::Extensions
2
2
  module PluginsMap
3
+ # provide extra map of plugins with symbolized names as keys
4
+ #
5
+ # @param type [String] name of type to generate the map for
6
+ # @return [Hash] map of the names and plugin classes
3
7
  def plugins_map( type )
4
8
  @plugins_map ||= {}
5
9
  type = type.to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluginator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.3
4
+ version: 0.11.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Papis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-11 00:00:00.000000000 Z
11
+ date: 2013-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake