pluginator 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +10 -0
  3. data/README.md +7 -5
  4. data/lib/pluginator/autodetect/finder.rb +85 -0
  5. data/lib/pluginator/autodetect/formatted_finder.rb +110 -0
  6. data/lib/pluginator/autodetect.rb +41 -73
  7. data/lib/pluginator/errors.rb +3 -2
  8. data/lib/pluginator/extendable_autodetect.rb +5 -9
  9. data/lib/pluginator/name_converter.rb +6 -16
  10. data/lib/pluginator/version.rb +2 -2
  11. data/lib/pluginator.rb +2 -2
  12. data/lib/plugins/pluginator/extensions/conversions.rb +8 -5
  13. data/lib/plugins/pluginator/extensions/first_ask.rb +2 -2
  14. data/lib/plugins/pluginator/extensions/first_class.rb +3 -3
  15. data/lib/plugins/pluginator/extensions/plugins_map.rb +8 -3
  16. metadata +43 -103
  17. data/.gitignore +0 -5
  18. data/.noexec.yaml +0 -1
  19. data/.travis.yml +0 -26
  20. data/Gemfile +0 -34
  21. data/Guardfile +0 -17
  22. data/Rakefile +0 -49
  23. data/pluginator.gemspec +0 -44
  24. data/test/gems/gems/fake-gem-name-a-1.0.0/lib/fake-gem-name-a.rb +0 -1
  25. data/test/gems/gems/fake-gem-name-a-1.0.0/lib/plugins/something/math/decrease.rb +0 -27
  26. data/test/gems/gems/fake-gem-name-a-1.0.0/lib/plugins/something/math/increase.rb +0 -27
  27. data/test/gems/gems/fake-gem-name-a-1.0.0/lib/plugins/something/nested/structure/test.rb +0 -25
  28. data/test/gems/gems/fake-gem-name-a-1.0.0/lib/plugins/something/stats/max.rb +0 -29
  29. data/test/gems/gems/fake-gem-name-activated-1.0.0/lib/fake-gem-name-activated.rb +0 -1
  30. data/test/gems/gems/fake-gem-name-activated-1.0.0/lib/plugins/activated/version1/max.rb +0 -29
  31. data/test/gems/gems/fake-gem-name-activated-2.0.0/lib/fake-gem-name-activated.rb +0 -1
  32. data/test/gems/gems/fake-gem-name-activated-2.0.0/lib/plugins/activated/version1/max.rb +0 -29
  33. data/test/gems/gems/fake-gem-name-latest-1.0.0/lib/fake-gem-name-latest.rb +0 -1
  34. data/test/gems/gems/fake-gem-name-latest-1.0.0/lib/plugins/latest/version1/max.rb +0 -29
  35. data/test/gems/gems/fake-gem-name-latest-2.0.0/lib/fake-gem-name-latest.rb +0 -1
  36. data/test/gems/gems/fake-gem-name-latest-2.0.0/lib/plugins/latest/version2/max.rb +0 -29
  37. data/test/gems/gems/fake-gem-name-v1a-1.0.0/lib/fake-gem-name-v1a.rb +0 -1
  38. data/test/gems/gems/fake-gem-name-v1a-1.0.0/lib/plugins/v1test/stats/max.rb +0 -29
  39. data/test/gems/gems/fake-gem-name-v1b-1.0.0/lib/fake-gem-name-v1a.rb +0 -1
  40. data/test/gems/gems/fake-gem-name-v1b-1.0.0/lib/plugins/v1test/stats/max.rb +0 -29
  41. data/test/gems/gems/fake-gem-name-v2a-1.0.0/lib/fake-gem-name-v2a.rb +0 -1
  42. data/test/gems/gems/fake-gem-name-v2a-1.0.0/lib/plugins/v2test/stats/max.rb +0 -29
  43. data/test/gems/gems/fake-gem-name-v2b-1.0.0/lib/fake-gem-name-v2b.rb +0 -1
  44. data/test/gems/gems/fake-gem-name-v2b-1.0.0/lib/plugins/v2test/stats/max.rb +0 -29
  45. data/test/gems/specifications/fake-gem-name-a-1.0.0.gemspec +0 -21
  46. data/test/gems/specifications/fake-gem-name-activated-1.0.0.gemspec +0 -21
  47. data/test/gems/specifications/fake-gem-name-activated-2.0.0.gemspec +0 -21
  48. data/test/gems/specifications/fake-gem-name-latest-1.0.0.gemspec +0 -21
  49. data/test/gems/specifications/fake-gem-name-latest-2.0.0.gemspec +0 -21
  50. data/test/gems/specifications/fake-gem-name-v1a-1.0.0.gemspec +0 -21
  51. data/test/gems/specifications/fake-gem-name-v1b-1.0.0.gemspec +0 -24
  52. data/test/gems/specifications/fake-gem-name-v2a-1.0.0.gemspec +0 -24
  53. data/test/gems/specifications/fake-gem-name-v2b-1.0.0.gemspec +0 -24
  54. data/test/pluginator/autodetect_test.rb +0 -170
  55. data/test/pluginator/extendable_autodetect_test.rb +0 -43
  56. data/test/pluginator/group_test.rb +0 -45
  57. data/test/pluginator/name_converter_test.rb +0 -52
  58. data/test/pluginator_test.rb +0 -132
  59. data/test/plugins_test/extensions/class_exist_test.rb +0 -45
  60. data/test/plugins_test/extensions/conversions_test.rb +0 -34
  61. data/test/plugins_test/extensions/first_ask_test.rb +0 -63
  62. data/test/plugins_test/extensions/first_class_test.rb +0 -63
  63. data/test/plugins_test/extensions/matching_test.rb +0 -55
  64. data/test/plugins_test/extensions/plugins_map_test.rb +0 -35
  65. data/test/test_helper.rb +0 -88
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6ff46b640ad85f210acebe6882c7ea4cbdf1c0c
4
- data.tar.gz: 447d3308d735dd4383a3300294c67ba065d7b986
3
+ metadata.gz: 283439793e08107d8bae13b5f46fc0da05785dd8
4
+ data.tar.gz: 80576d911e37400591f350ad35249ba3dbe0d7ec
5
5
  SHA512:
6
- metadata.gz: 40ea8041c8c585023ef5bc68c623765ca25dc4f002f851bd7a4308f78fd9f6e8865f57f0c6d934720009594e5c6bb05bb2fc4f5539c1f274961212e41e13ce66
7
- data.tar.gz: 83d9f189e3b9ab2beb65d87b6da06198a191bdf6e3e5841260295273c08180aaa1e783a4c241ba69810de86989e687d820bb05cd6868cecb8c0cd3d122c0fff7
6
+ metadata.gz: '094f7696b9db9e54bf55da059a5c0ab0b184d05c78caf2ec3ab288dd4cb63d8893f632654ead6c5dee696823f2893216e779d79775f37ccded0c5d7438fdb2f2'
7
+ data.tar.gz: a059c11a3ebd3e61093bf37e068c3fde86ff7b7efcb05af3ea5f840bcee515acabb5e7e91d8c74e5817f7430c7aecb031c6a258116893277382cc4e45400a2ab
data/Changelog.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.0
4
+ date: 2017-05-14
5
+
6
+ - reworked finding plugins, now also searches `$LOADED_FEATURES`
7
+
8
+ ## 1.3.0
9
+ date: 2014-10-20
10
+
11
+ - simplified detection of gems/plugins to load, improved tests
12
+
3
13
  ## 1.2.0
4
14
  date: 2014-09-24
5
15
 
data/README.md CHANGED
@@ -9,8 +9,10 @@
9
9
  [![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/rvm/pluginator/master/frames)
10
10
  [![Github Code](http://img.shields.io/badge/github-code-blue.svg)](https://github.com/rvm/pluginator)
11
11
 
12
- Gem plugin system management, detects plugins using `Gem.find_file`.
13
- Is only supposed to work with ruby 1.9.3+ (some tests fail on 1.9.2)
12
+ Gem plugin system management, detects plugins using `Gem.find_file`,
13
+ `$LOAD_PATH` and `$LOADED_FEATURES`.
14
+
15
+ It is only supposed to work with ruby 1.9.3+ (some tests fail on 1.9.2)
14
16
 
15
17
  Pluginator tries to stay out of your way, you do not have to include or inherit anything.
16
18
  Pluginator only finds and groups plugins, rest is up to you,
@@ -21,7 +23,7 @@ you decide what methods to define and how to find them.
21
23
  create a gem with a path:
22
24
 
23
25
  ```ruby
24
- plugins/<group>/<type>/<name>.rb
26
+ lib/plugins/<group>/<type>/<name>.rb
25
27
  ```
26
28
 
27
29
  with a class inside:
@@ -133,7 +135,7 @@ plugins.first_one("<type>") => first_plugin # nil when none
133
135
 
134
136
  ## Versioning plugins
135
137
 
136
- In case plugin gets moved to other gem you can sdpecify which gem to
138
+ In case plugin gets moved to other gem you can specify which gem to
137
139
  use for loading the plugin by specifying plugin version in gems gemspec
138
140
  metadata:
139
141
 
@@ -213,7 +215,7 @@ NOEXEC_DISABLE=1 rake test
213
215
 
214
216
  ## License
215
217
 
216
- Copyright 2013 Michal Papis <mpapis@gmail.com>
218
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
217
219
 
218
220
  pluginator is free software: you can redistribute it and/or modify
219
221
  it under the terms of the GNU Lesser General Public License as published
@@ -0,0 +1,85 @@
1
+ =begin
2
+ Copyright 2017 Michal Papis <mpapis@gmail.com>
3
+
4
+ This file is part of pluginator.
5
+
6
+ pluginator is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published
8
+ by the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ pluginator is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with pluginator. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ module Pluginator
21
+ class Autodetect
22
+
23
+ # Find plugins
24
+ class Finder
25
+
26
+ attr_reader :loaded_plugins_path, :load_path_plugins_paths, :gem_plugins_paths
27
+
28
+ # Automatically load plugins for given group (and type)
29
+ #
30
+ # @param group [String] name of the plugins group
31
+ # @param force_type [String] name of the plugin type if forcing
32
+ def initialize(group, force_type)
33
+ @group = group
34
+ @force_type = force_type
35
+ @pattern = file_name_pattern(@group, @force_type)
36
+ find_paths
37
+ end
38
+
39
+ private
40
+
41
+ # group => pattern
42
+ def file_name_pattern(group, type=nil)
43
+ "plugins/#{group}/#{type || "**"}/*.rb"
44
+ end
45
+
46
+ def find_paths
47
+ @loaded_plugins_path = find_loaded_plugins
48
+ @load_path_plugins_paths = find_load_path_plugins - @loaded_plugins_path
49
+ @gem_plugins_paths = find_gem_plugins - @load_path_plugins_paths - @loaded_plugins_path
50
+ end
51
+
52
+ def find_loaded_plugins
53
+ split_file_names(
54
+ $LOADED_FEATURES
55
+ ).compact
56
+ end
57
+
58
+ def find_load_path_plugins
59
+ split_file_names(
60
+ Gem.find_files_from_load_path(@pattern)
61
+ )
62
+ end
63
+
64
+ def find_gem_plugins
65
+ split_file_names(
66
+ Gem.find_files(@pattern, false)
67
+ )
68
+ end
69
+
70
+ def split_file_names(file_names)
71
+ file_names.map do |file_name|
72
+ split_file_name(file_name, @group, @force_type)
73
+ end
74
+ end
75
+
76
+ # file_name, group, type => [ path, full_name, type ]
77
+ def split_file_name(file_name, group, type=nil)
78
+ type ||= ".*"
79
+ match = file_name.match(%r{.*/(plugins/(#{group}/(#{type})/[^/]*)\.rb)$})
80
+ match[1..3] if match
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,110 @@
1
+ =begin
2
+ Copyright 2017 Michal Papis <mpapis@gmail.com>
3
+
4
+ This file is part of pluginator.
5
+
6
+ pluginator is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published
8
+ by the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ pluginator is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with pluginator. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ require "pluginator/autodetect/finder"
21
+
22
+ module Pluginator
23
+ class Autodetect
24
+
25
+ # Categorize plugins
26
+ # @see: Finder
27
+ class FormattedFinder < Finder
28
+
29
+ # Reformat plugin lists
30
+ def initialize(group, force_type)
31
+ super
32
+ map_loaded_plugins
33
+ map_gem_plugins
34
+ end
35
+
36
+ private
37
+
38
+ def gem_has_method?(name)
39
+ Gem.methods.map(&:to_sym).include?(name)
40
+ end
41
+
42
+ def map_loaded_plugins
43
+ @loaded_plugins_path.map! do |_path, name, type|
44
+ [name, type]
45
+ end
46
+ end
47
+
48
+ def map_gem_plugins
49
+ gem_specifications = find_gem_specifications
50
+ @gem_plugins_paths.map! do |path, name, type|
51
+ gemspec = gemspec_for_path(path, gem_specifications)
52
+ [gemspec, path, name, type] if gemspec
53
+ end.compact
54
+ end
55
+
56
+ def find_gem_specifications
57
+ if
58
+ gem_has_method?(:gemdeps) && Gem.gemdeps
59
+ then
60
+ # :nocov: only testable with using rubygems's gemdeps feature
61
+ Gem.loaded_specs.values
62
+ # :nocov:
63
+ else
64
+ specs = Gem::Specification._all
65
+ specs = (Gem.loaded_specs.values + specs).uniq if gem_has_method?(:loaded_specs)
66
+ specs
67
+ end
68
+ end
69
+
70
+ def gemspec_for_path(path, specifications)
71
+ gemspecs = gemspecs_for_path(path, specifications)
72
+ case
73
+ gemspecs.size
74
+ when 0
75
+ nil
76
+ when 1
77
+ gemspecs.first
78
+ else
79
+ find_latest_plugin_version(gemspecs, path)
80
+ end
81
+ end
82
+
83
+ def gemspecs_for_path(path, specifications)
84
+ specifications.reject do |spec|
85
+ Dir.glob( File.join( spec.lib_dirs_glob, path ) ).empty?
86
+ end
87
+ end
88
+
89
+ def find_latest_plugin_version(gemspecs, path)
90
+ active_or_latest_gemspec(gemspecs_sorted_by_metadata_and_version(gemspecs, path))
91
+ end
92
+
93
+ # find active or latest gem in given set
94
+ def active_or_latest_gemspec(specifications)
95
+ specifications.find(&:activated) || specifications.last
96
+ end
97
+
98
+ def gemspecs_sorted_by_metadata_and_version(gemspecs, path)
99
+ gemspecs.sort_by do |spec|
100
+ [calculate_plugin_version(spec.metadata, path), spec.name, spec.version]
101
+ end
102
+ end
103
+
104
+ def calculate_plugin_version(metadata, path)
105
+ ( (metadata || {})[path] || "0" ).to_i
106
+ end
107
+
108
+ end
109
+ end
110
+ end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -22,18 +22,38 @@ require "pluginator/group"
22
22
  require "pluginator/name_converter"
23
23
 
24
24
  module Pluginator
25
- # Add autodetection capabilities to Group
26
- # @see Group
27
25
  class Autodetect < Group
26
+ end
27
+ end
28
+
29
+ require "pluginator/autodetect/formatted_finder"
30
+
31
+ module Pluginator
32
+ # Add autodetection capabilities to Group
33
+ # @see Group, FormattedFinder
34
+ class Autodetect
28
35
 
29
36
  # Automatically load plugins for given group (and type)
30
37
  #
31
38
  # @param group [String] name of the plugins group
32
39
  # @param options [Hash] options to pass to creating Pluginator instance
33
40
  # @option type [String] name of the plugin type
34
- def initialize(group, options = {})
41
+ def initialize(group, options={})
35
42
  super(group)
36
- setup_autodetect(options[:type])
43
+ @force_type = options[:type]
44
+ refresh
45
+ end
46
+
47
+ # Initiate another lookup for plugins
48
+ # - does not clean the state
49
+ # - does not resolve all gems, only the new ones
50
+ #
51
+ # Use it after gem list change, for example after `Gem.install("new_gem")`
52
+ def refresh
53
+ plugin_lists = FormattedFinder.new(@group, @force_type)
54
+ register_plugins(plugin_lists.loaded_plugins_path)
55
+ load_plugins(plugin_lists.load_path_plugins_paths)
56
+ activate_plugins(plugin_lists.gem_plugins_paths)
37
57
  end
38
58
 
39
59
  # Return the forced type
@@ -45,39 +65,33 @@ module Pluginator
45
65
 
46
66
  include NameConverter
47
67
 
48
- def setup_autodetect(type)
49
- force_type(type)
50
- load_files(find_files)
51
- end
52
-
53
- def force_type(type)
54
- @force_type = type
55
- end
56
-
57
- def find_files
58
- Gem.find_files(file_name_pattern(@group, @force_type))
68
+ def register_plugins(plugins_to_register)
69
+ plugins_to_register.each do |name, type|
70
+ register_plugin(type, name2class(name))
71
+ end
59
72
  end
60
73
 
61
- def load_files(file_names)
62
- unique_gemspec_paths(file_names).each do |gemspec, path, name, type|
63
- gemspec.activate
64
- load_plugin(path) and
74
+ def load_plugins(plugins_to_load)
75
+ plugins_to_load.each do |path, name, type|
76
+ require path
65
77
  register_plugin(type, name2class(name))
66
78
  end
67
79
  end
68
80
 
69
- def unique_gemspec_paths(file_names)
70
- all = gemspec_and_paths(file_names)
71
- selected = active_or_latest_gems_matching(all.map(&:first))
72
- all.select do |gemspec, path, name, type|
73
- selected.include?(gemspec)
81
+ def activate_plugins(plugins_to_activate)
82
+ selected = active_or_latest_gems_matching(plugins_to_activate.map(&:first).compact)
83
+ plugins_to_activate.each do |gemspec, path, name, type|
84
+ next unless selected.include?(gemspec)
85
+ gemspec.activate
86
+ require path
87
+ register_plugin(type, name2class(name))
74
88
  end
75
89
  end
76
90
 
77
91
  # filter active / latest gem versions
78
92
  def active_or_latest_gems_matching(specifications)
79
- specifications.group_by(&:name).map do |name, specifications|
80
- active_or_latest_gemspec(specifications.sort)
93
+ specifications.group_by(&:name).map do |_name, plugin_specifications|
94
+ active_or_latest_gemspec(plugin_specifications.sort)
81
95
  end
82
96
  end
83
97
 
@@ -86,51 +100,5 @@ module Pluginator
86
100
  specifications.find(&:activated) || specifications.last
87
101
  end
88
102
 
89
- def gemspec_and_paths(file_names)
90
- unique_parsed_paths(file_names).map do |path, name, type|
91
- gemspec = gemspec_for_path(path)
92
- [gemspec, path, name, type] if gemspec
93
- end.compact
94
- end
95
-
96
- def unique_parsed_paths(file_names)
97
- file_names.map do |file_name|
98
- split_file_name(file_name, @group)
99
- end.sort.uniq
100
- end
101
-
102
- def gemspec_for_path(path)
103
- gemspecs = Gem::Specification._all.reject do |spec|
104
- Dir.glob( File.join( spec.lib_dirs_glob, path ) ).empty?
105
- end
106
- case gemspecs.size
107
- when 0
108
- nil
109
- when 1
110
- gemspecs.first
111
- else
112
- find_latest_plugin_version(gemspecs, path)
113
- end
114
- end
115
-
116
- def find_latest_plugin_version(gemspecs, path)
117
- active_or_latest_gemspec(gemspecs_sorted_by_metadata_and_version(gemspecs, path))
118
- end
119
-
120
- def gemspecs_sorted_by_metadata_and_version(gemspecs, path)
121
- gemspecs.sort_by do |spec|
122
- [calculate_plugin_version(spec.metadata, path), spec.name, spec.version]
123
- end
124
- end
125
-
126
- def calculate_plugin_version(metadata, path)
127
- ((metadata||{})[path]||"0").to_i
128
- end
129
-
130
- def load_plugin(path)
131
- require path
132
- true
133
- end
134
-
135
103
  end
136
104
  end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -22,8 +22,9 @@ module Pluginator
22
22
  # base error for all Pluginator errors
23
23
  class PluginatorError < RuntimeError
24
24
  private
25
+
25
26
  def list_to_s(list)
26
- list.map{|e| e.to_s.inspect }.join(", ")
27
+ list.map { |plugin| plugin.to_s.inspect }.join(", ")
27
28
  end
28
29
  end
29
30
 
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -38,9 +38,9 @@ module Pluginator
38
38
  # @param options [Hash] options to pass to creating Pluginator instance
39
39
  # @option type [String] name of type to load
40
40
  # @option extends [Array<Symbol>|Symbol] list of extension to extend into pluginator instance
41
- def initialize(group, options = {})
41
+ def initialize(group, options={})
42
42
  super(group, options)
43
- extend_plugins(options[:extends]||[])
43
+ extend_plugins(options[:extends] || [])
44
44
  end
45
45
 
46
46
  # Extend pluginator instance with given extensions
@@ -54,11 +54,6 @@ module Pluginator
54
54
 
55
55
  private
56
56
 
57
- def flatten_and_stringify(extends)
58
- extends = [extends].flatten.map(&:to_s)
59
- extends
60
- end
61
-
62
57
  def pluginator_plugins
63
58
  @pluginator_plugins ||= begin
64
59
  plugins = Pluginator::Autodetect.new("pluginator")
@@ -68,7 +63,8 @@ module Pluginator
68
63
  end
69
64
 
70
65
  def extensions_matching(extends)
71
- pluginator_plugins.matching!("extensions", flatten_and_stringify(extends))
66
+ extends = [extends].flatten.map(&:to_s)
67
+ pluginator_plugins.matching!("extensions", extends)
72
68
  end
73
69
 
74
70
  end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -22,24 +22,14 @@ module Pluginator
22
22
  module NameConverter
23
23
  private
24
24
 
25
- # file_name, group => [ path, full_name, type ]
26
- def split_file_name(file_name, group)
27
- file_name.match(/.*\/(plugins\/(#{group}\/(.*)\/[^\/]*)\.rb)$/)[1..3]
28
- end
29
-
30
- # group => pattern
31
- def file_name_pattern(group, type=nil)
32
- "plugins/#{group}/#{type || "**"}/*.rb"
33
- end
34
-
35
25
  # full_name => class
36
26
  def name2class(name)
37
27
  klass = Kernel
38
- name.to_s.split(/\//).map{ |part|
39
- part.capitalize.gsub(/_(.)/){ $1.upcase }
40
- }.each{|part|
41
- klass = klass.const_get( part )
42
- }
28
+ name.to_s.split(%r{/}).each do |part|
29
+ klass = klass.const_get(
30
+ part.capitalize.gsub(/_(.)/) { |match| match[1].upcase }
31
+ )
32
+ end
43
33
  klass
44
34
  end
45
35
 
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -19,5 +19,5 @@ along with pluginator. If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
  module Pluginator
21
21
  # Version of Pluginator
22
- VERSION = "1.3.0"
22
+ VERSION = "1.4.0".freeze
23
23
  end
data/lib/pluginator.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -29,7 +29,7 @@ module Pluginator
29
29
  # @option type [String] name of type to load
30
30
  # @option extend [Array<Symbol>|Symbol] list of extension to extend into pluginator instance
31
31
  # @return [Pluginator::ExtendableAutodetect] instance of Pluginator
32
- def self.find(group, options = {})
32
+ def self.find(group, options={})
33
33
  Pluginator::ExtendableAutodetect.new(group, options)
34
34
  end
35
35
  end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -24,20 +24,23 @@ module Pluginator::Extensions
24
24
  # converts class name to a file name
25
25
  # @param klass [String] class like string
26
26
  # @return [String] file like string
27
- def class2string( klass )
28
- klass.to_s.gsub(/([A-Z])/m){|match| "_#{$1.downcase}" }[1..-1]
27
+
28
+ def class2string(klass)
29
+ klass.to_s.gsub(/([A-Z])/m) { |match| "_#{match.downcase}" }[1..-1]
29
30
  end
30
31
 
31
32
  # converts file name to a class name
32
33
  # @param str [String] file like string
33
34
  # @return [String] class like string
34
- def string2class( str )
35
- str.to_s.capitalize.gsub(/_(.)/){ $1.upcase }
35
+
36
+ def string2class(str)
37
+ str.to_s.capitalize.gsub(/_(.)/) { |match| match[1].upcase }
36
38
  end
37
39
 
38
40
  # gets class name last part
39
41
  # @param klass [Class] class to read
40
42
  # @return [String] the class name only without nesting
43
+
41
44
  def class2name(klass)
42
45
  klass.name.split("::").last
43
46
  end
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -53,7 +53,7 @@ module Pluginator::Extensions
53
53
 
54
54
  def try_to_find(type, method_name, params)
55
55
  @plugins[type].detect do |plugin|
56
- has_public_method?(plugin, method_name) &&
56
+ has_public_method?(plugin, method_name) and
57
57
  plugin.send(method_name.to_sym, *params)
58
58
  end
59
59
  end
@@ -21,13 +21,13 @@ require "plugins/pluginator/extensions/plugins_map"
21
21
  require "plugins/pluginator/extensions/conversions"
22
22
 
23
23
  module Pluginator::Extensions
24
- # Extension to find first plugin that class matches the string
24
+ # Extension to find first plugin whose class matches the string
25
25
  module FirstClass
26
26
 
27
27
  include PluginsMap
28
28
  include Conversions
29
29
 
30
- # Find first plugin that clas matches the given name.
30
+ # Find first plugin whose class matches the given name.
31
31
  #
32
32
  # @param type [String] name of type to search for plugins
33
33
  # @param klass [Symbol|String] name of the searched class
@@ -36,7 +36,7 @@ module Pluginator::Extensions
36
36
  (plugins_map(type) || {})[string2class(klass)]
37
37
  end
38
38
 
39
- # Find first plugin that clas matches the given name.
39
+ # Find first plugin whose class matches the given name.
40
40
  # Behaves like `first_class` but throws exceptions if can not find anything.
41
41
  # @param type [String] name of type to search for plugins
42
42
  # @param klass [Symbol|String] name of the searched class
@@ -1,5 +1,5 @@
1
1
  =begin
2
- Copyright 2013 Michal Papis <mpapis@gmail.com>
2
+ Copyright 2013-2017 Michal Papis <mpapis@gmail.com>
3
3
 
4
4
  This file is part of pluginator.
5
5
 
@@ -28,10 +28,15 @@ module Pluginator::Extensions
28
28
  #
29
29
  # @param type [String] name of type to generate the map for
30
30
  # @return [Hash] map of the names and plugin classes
31
- def plugins_map( type )
31
+
32
+ def plugins_map(type)
32
33
  @plugins_map ||= {}
33
34
  type = type.to_s
34
- @plugins_map[type] ||= Hash[ @plugins[type].map{|plugin| [class2name(plugin), plugin] } ]
35
+ @plugins_map[type] ||= Hash[
36
+ @plugins[type].map do |plugin|
37
+ [class2name(plugin), plugin]
38
+ end
39
+ ]
35
40
  end
36
41
  end
37
42
  end