pluginator 0.9.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c2d1a80503ff918471f6e8477ac36987d7ae8bf
4
+ data.tar.gz: 0902865fc2f708aeb692a647cb4c0a8af808ecc1
5
+ SHA512:
6
+ metadata.gz: ef24dd58904758e1ab42b0f8db5c6677b4c5d3ac0aca1551984c294cc06c0138282de6a5aaecb2e73540b8941399ebfd9e8352f221cc84dcb431e79f33c54c4b
7
+ data.tar.gz: 297af16db59fb91ae6b9fa1c66bff269bb38da2be00e13c87050ef1fa48f706af74deae591be4d0ef2ac7cd772d9bd9de8c2c91bd39a9db6eca14107b8705d73
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ notifications:
8
+ irc:
9
+ channels:
10
+ - "irc.freenode.org#rvm-test"
11
+ email:
12
+ recipients:
13
+ - mpapis@gmail.com
14
+ on_failure: change
data/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
- #ruby=1.9.3
3
+ #ruby=2.0.0
4
4
 
5
5
  gemspec
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Pluginator
2
2
 
3
3
  Gem plugin system management, detects plugins using `Gem.find_file`.
4
- Is only supposed with ruby 1.9.2+
4
+ Is only supposed with ruby 1.9.3+
5
5
 
6
6
  Pluginator tries to stay out of your way, you do not have to include or inherit anything.
7
7
  Pluginator only finds and groups plugins, rest is up to you,
@@ -12,13 +12,13 @@ you decide what methods to define and how to find them.
12
12
  crate a gem with a path:
13
13
 
14
14
  ```ruby
15
- plugins/<prefix>/<type>/<name>.rb
15
+ plugins/<group>/<type>/<name>.rb
16
16
  ```
17
17
 
18
18
  with a class inside:
19
19
 
20
20
  ```ruby
21
- <prefix>::<type>::<name>
21
+ <group>::<type>::<name>
22
22
  ```
23
23
 
24
24
  where `<type>` can be nested
@@ -26,7 +26,7 @@ where `<type>` can be nested
26
26
  ## Loading plugins
27
27
 
28
28
  ```ruby
29
- rvm2plugins = Pluginator.new("<prefix>")
29
+ rvm2plugins = Pluginator::Autodetect.new("<group>")
30
30
  type_plugins = rvm2plugins["<type>"]
31
31
  types = rvm2plugins.types
32
32
  ```
@@ -55,7 +55,7 @@ Now the plugin can be used:
55
55
  ```ruby
56
56
  require 'pluginator'
57
57
 
58
- rvm2plugins = Pluginator.new("rvm2")
58
+ rvm2plugins = Pluginator::Autodetect.new("rvm2")
59
59
  plugin = rvm2plugins["cli"].first{ |plugin|
60
60
  plugin.question?('echo')
61
61
  }
@@ -79,7 +79,7 @@ and using hooks:
79
79
  ```ruby
80
80
  require 'pluginator'
81
81
 
82
- rvm2plugins = Pluginator.new("rvm2")
82
+ rvm2plugins = Pluginator::Autodetect.new("rvm2")
83
83
  plugin = rvm2plugins["hooks/after_install"].each{ |plugin|
84
84
  plugin.execute(name, path)
85
85
  }
data/Rakefile CHANGED
@@ -1,4 +1,9 @@
1
1
  require "rake/testtask"
2
+
2
3
  task :default => [:test]
3
- task :spec => :test
4
- Rake::TestTask.new { |t| t.verbose, t.pattern = true, "test/**/test_*.rb" }
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.verbose = true
7
+ t.libs.push("demo")
8
+ t.pattern = "test/**/*_test.rb"
9
+ end
@@ -0,0 +1,8 @@
1
+ class Pluginator::Math::Decrease
2
+ def self.type
3
+ "decrease"
4
+ end
5
+ def self.action(number)
6
+ number - 1
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class Pluginator::Math::Increase
2
+ def self.type
3
+ "increase"
4
+ end
5
+ def self.action(number)
6
+ number + 1
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "group"
2
+ require_relative "name_converter"
3
+
4
+ module Pluginator
5
+ class Autodetect < Group
6
+
7
+ def initialize(group)
8
+ super(group)
9
+ load_files(find_files)
10
+ end
11
+
12
+ private
13
+
14
+ include NameConverter
15
+
16
+ def load_files(file_names)
17
+ file_names.each do |file_name|
18
+ path, name, type = split_file_name(file_name, @group)
19
+ load_plugin path
20
+ register_plugin(type, name2class(name))
21
+ end
22
+ end
23
+
24
+ def find_files
25
+ Gem.find_files(file_name_pattern(@group))
26
+ end
27
+
28
+ def load_plugin(path)
29
+ gemspec = Gem::Specification.find_by_path(path)
30
+ gemspec.activate if gemspec && !gemspec.activated?
31
+ require path
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ module Pluginator
2
+ class Group
3
+ attr_reader :group
4
+
5
+ def initialize(group)
6
+ setup(group)
7
+ end
8
+
9
+ def [](type)
10
+ @plugins[type.to_s]
11
+ end
12
+
13
+ def types
14
+ @plugins.keys
15
+ end
16
+
17
+ def register_plugin(type, klass)
18
+ type = type.to_s
19
+ @plugins[type] ||= []
20
+ @plugins[type].push(klass)
21
+ end
22
+
23
+ private
24
+
25
+ def setup(group)
26
+ @plugins = {}
27
+ @group = group
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module Pluginator
2
+ module NameConverter
3
+ private
4
+
5
+ # file_name, group => [ path, full_name, type ]
6
+ def split_file_name(file_name, group)
7
+ file_name.match(/.*\/(plugins\/(#{group}\/(.*)\/[^\/]*)\.rb)$/)[1..3]
8
+ end
9
+
10
+ # group => pattern
11
+ def file_name_pattern(group)
12
+ "plugins/#{group}/**/*.rb"
13
+ end
14
+
15
+ # full_name => class
16
+ def name2class(name)
17
+ klass = Kernel
18
+ name.to_s.split(/\//).map{ |part|
19
+ part.capitalize.gsub(/_(.)/){ $1.upcase }
20
+ }.each{|part|
21
+ klass = klass.const_get( part )
22
+ }
23
+ klass
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Pluginator
2
+ VERSION = "0.10.0"
3
+ end
data/lib/pluginator.rb CHANGED
@@ -1,94 +1,2 @@
1
- class Pluginator
2
- attr_reader :plugins_name
3
-
4
- def initialize(plugins_name)
5
- @plugins = {}
6
- @plugins_name = plugins_name
7
- detect
8
- end
9
-
10
- def first_plugin type, method, *args
11
- result = nil
12
- name, plugin = all_by_type(type).detect do |name, plugin|
13
- result = plugin.send(method.to_sym, *args)
14
- end
15
- [ name, plugin, result ]
16
- end
17
-
18
- def all_by_type type
19
- @plugins[type.to_s]
20
- end
21
- alias :[] :all_by_type
22
-
23
- def types
24
- @plugins.keys
25
- end
26
-
27
- # TODO do we need those???
28
- def all_plugins type, method, *args
29
- @plugins[type.to_s].select do |name,plugin|
30
- plugin.send(method.to_sym, *args)
31
- end
32
- end
33
- def first_plugin_by_parent parent_class
34
- @plugins.detect do |type, plugins|
35
- plugins.detect do |name, plugin|
36
- plugin < parent_class
37
- end
38
- end
39
- end
40
- def find_class_by_parent parent_class
41
- classes = []
42
- ObjectSpace.each_object(Class) do |klass|
43
- classes << klass if klass < parent_class
44
- end
45
- classes
46
- end
47
-
48
- def register_plugin_class type, klass
49
- type = type.to_s
50
- name = class2name(klass)
51
- @plugins[type] ||= {}
52
- if @plugins[type][name].nil?
53
- @plugins[type][name] = klass
54
- end
55
- end
56
-
57
- private
58
-
59
- def detect
60
- Gem.find_files("plugins/#{@plugins_name}/*/*.rb").each do |file_name|
61
- path, name, type = file_name.match(/.*\/(plugins\/(#{@plugins_name}\/(.*)\/[^\/]*)\.rb)$/)[1..3]
62
- load_plugin path, file_name
63
- register_plugin type, name
64
- end
65
- end
66
-
67
- def load_plugin path, file_name
68
- gemspec = Gem::Specification.find_by_path(path)
69
- gemspec.activate if gemspec && !gemspec.activated?
70
- require path
71
- end
72
-
73
- def register_plugin type, name
74
- type = type.to_s
75
- @plugins[type] ||= {}
76
- if @plugins[type][name].nil?
77
- @plugins[type][name] = name2class(name)
78
- end
79
- end
80
-
81
- def name2class name
82
- klass = Kernel
83
- name.split(/\//).map{ |part|
84
- part.capitalize.gsub(/_(.)/){ $1.upcase }
85
- }.each{|part|
86
- klass = klass.const_get( part )
87
- }
88
- klass
89
- end
90
-
91
- def class2name klass
92
- klass.gsub(/([A-Z])/){|x| "_#{x.downcase}"}.gsub(/(^_|::_)/,"/")[1..-1]
93
- end
94
- end
1
+ require_relative "pluginator/autodetect"
2
+ require_relative "pluginator/version"
data/pluginator.gemspec CHANGED
@@ -1,18 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- encoding: utf-8 -*-
3
3
 
4
+ require File.expand_path("../lib/pluginator/version.rb", __FILE__)
5
+
4
6
  Gem::Specification.new do |s|
5
7
  s.email = ["mpapis@gmail.com"]
6
8
  s.authors = ["Michal Papis"]
7
9
  s.name = "pluginator"
8
- s.version = "0.9.3"
10
+ s.version = Pluginator::VERSION
9
11
  s.files = `git ls-files`.split("\n")
10
- s.required_ruby_version = ">= 1.9.2"
11
- s.add_development_dependency("simplecov")
12
+ s.required_ruby_version = ">= 1.9.3"
12
13
  s.add_development_dependency("rake")
13
14
  s.add_development_dependency("minitest")
14
15
  # s.add_development_dependency("smf-gem")
15
- s.homepage = "https://github.com/mpapis/pluginator"
16
+ s.homepage = "https://github.com/rvm/pluginator"
16
17
  s.summary = "Rubygems plugin system using Gem.find_files."
17
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
19
  end
@@ -0,0 +1,75 @@
1
+ require 'minitest/autorun'
2
+ require 'pluginator/autodetect'
3
+
4
+ def demo_file(path)
5
+ File.expand_path(File.join("..", "..", "demo", path), __FILE__)
6
+ end
7
+
8
+ def demo_files(*paths)
9
+ paths.flatten.map{|path| demo_file(path) }
10
+ end
11
+
12
+ module Pluginator::Math; end
13
+
14
+ describe Pluginator::Autodetect do
15
+ before do
16
+ @demo_files = demo_files("plugins/pluginator/math/increase.rb", "plugins/pluginator/math/decrease.rb")
17
+ end
18
+
19
+ describe "separate" do
20
+ before do
21
+ @pluginator = Pluginator::Autodetect.allocate
22
+ @pluginator.send(:setup, "pluginator")
23
+ end
24
+
25
+ it "has name" do
26
+ @pluginator.group.must_equal("pluginator")
27
+ end
28
+
29
+ it "has demo file" do
30
+ File.exists?(demo_file("plugins/pluginator/math/increase.rb")).must_equal(true)
31
+ end
32
+
33
+ it "loads file" do
34
+ @pluginator.send(:load_plugin, "plugins/pluginator/math/increase.rb")
35
+ end
36
+
37
+ it "finds files" do
38
+ @pluginator.send(:find_files).sort.must_equal(@demo_files.sort)
39
+ end
40
+
41
+ it "loads plugins" do
42
+ @pluginator.send(:load_files, @demo_files)
43
+ @pluginator.types.must_include('math')
44
+ plugins = @pluginator["math"].map(&:to_s)
45
+ plugins.size.must_equal(2)
46
+ plugins.must_include("Pluginator::Math::Increase")
47
+ plugins.must_include("Pluginator::Math::Decrease")
48
+ plugins.wont_include("Pluginator::Math::Substract")
49
+ end
50
+ end
51
+
52
+ it "loads plugins automatically" do
53
+ pluginator = Pluginator::Autodetect.new("pluginator")
54
+ pluginator.types.must_include('math')
55
+ plugins = pluginator["math"].map(&:to_s)
56
+ plugins.size.must_equal(2)
57
+ plugins.must_include("Pluginator::Math::Increase")
58
+ plugins.must_include("Pluginator::Math::Decrease")
59
+ plugins.wont_include("Pluginator::Math::Add")
60
+ end
61
+
62
+ it "makes plugins work" do
63
+ pluginator = Pluginator::Autodetect.new("pluginator")
64
+ pluginator["math"].detect{|plugin| plugin.type == "increase" }.action(2).must_equal(3)
65
+ pluginator["math"].detect{|plugin| plugin.type == "decrease" }.action(5).must_equal(4)
66
+ end
67
+
68
+ it "hides methods" do
69
+ pluginator = Pluginator::Autodetect.new("pluginator")
70
+ pluginator.public_methods.must_include(:register_plugin)
71
+ pluginator.public_methods.wont_include(:load_plugins)
72
+ pluginator.public_methods.wont_include(:split_file_name)
73
+ end
74
+
75
+ end
@@ -0,0 +1,25 @@
1
+ require 'minitest/autorun'
2
+ require 'pluginator/group'
3
+
4
+ describe Pluginator::Group do
5
+ it "has name" do
6
+ Pluginator::Group.new("something").group.must_equal("something")
7
+ end
8
+
9
+ describe "plugins list" do
10
+ before do
11
+ @group = Pluginator::Group.new("something")
12
+ end
13
+
14
+ it "adds type" do
15
+ @group.register_plugin("type1", nil)
16
+ @group.types.must_include("type1")
17
+ @group.types.wont_include("type2")
18
+ end
19
+ it "adds plugin" do
20
+ @group.register_plugin("type1", "test1")
21
+ @group["type1"].must_include("test1")
22
+ @group["type1"].wont_include("test2")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest/autorun'
2
+ require 'pluginator/name_converter'
3
+
4
+ class Converter
5
+ extend Pluginator::NameConverter
6
+ end
7
+
8
+ describe Pluginator::NameConverter do
9
+ describe "files" do
10
+ it "extracts file name components" do
11
+ Converter.send(:split_file_name, "/path/to/plugins/group1/type1/name1.rb", "group1").
12
+ must_equal(["plugins/group1/type1/name1.rb", "group1/type1/name1", "type1"])
13
+ end
14
+ it "builds group pattern" do
15
+ Converter.send(:file_name_pattern, "group2").must_equal("plugins/group2/**/*.rb")
16
+ end
17
+ end
18
+
19
+ describe "classes" do
20
+ it "builds class" do
21
+ Converter.send(:name2class, "Converter").must_equal(Converter)
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,62 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluginator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
5
- prerelease:
4
+ version: 0.10.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Michal Papis
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
11
+ date: 2013-05-09 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: simplecov
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: rake
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
- - - ! '>='
17
+ - - '>='
36
18
  - !ruby/object:Gem::Version
37
19
  version: '0'
38
20
  type: :development
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
- - - ! '>='
24
+ - - '>='
44
25
  - !ruby/object:Gem::Version
45
26
  version: '0'
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: minitest
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
30
  requirements:
51
- - - ! '>='
31
+ - - '>='
52
32
  - !ruby/object:Gem::Version
53
33
  version: '0'
54
34
  type: :development
55
35
  prerelease: false
56
36
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
37
  requirements:
59
- - - ! '>='
38
+ - - '>='
60
39
  - !ruby/object:Gem::Version
61
40
  version: '0'
62
41
  description:
@@ -67,37 +46,45 @@ extensions: []
67
46
  extra_rdoc_files: []
68
47
  files:
69
48
  - .gitignore
70
- - .rvmrc
49
+ - .travis.yml
71
50
  - Gemfile
72
51
  - README.md
73
52
  - Rakefile
53
+ - demo/plugins/pluginator/math/decrease.rb
54
+ - demo/plugins/pluginator/math/increase.rb
74
55
  - lib/pluginator.rb
75
- - lib/pluginator/plugin_handler.rb
56
+ - lib/pluginator/autodetect.rb
57
+ - lib/pluginator/group.rb
58
+ - lib/pluginator/name_converter.rb
59
+ - lib/pluginator/version.rb
76
60
  - pluginator.gemspec
77
- - test/test_paginator.rb
78
- homepage: https://github.com/mpapis/pluginator
61
+ - test/autodetect_test.rb
62
+ - test/group_test.rb
63
+ - test/name_converter_test.rb
64
+ homepage: https://github.com/rvm/pluginator
79
65
  licenses: []
66
+ metadata: {}
80
67
  post_install_message:
81
68
  rdoc_options: []
82
69
  require_paths:
83
70
  - lib
84
71
  required_ruby_version: !ruby/object:Gem::Requirement
85
- none: false
86
72
  requirements:
87
- - - ! '>='
73
+ - - '>='
88
74
  - !ruby/object:Gem::Version
89
- version: 1.9.2
75
+ version: 1.9.3
90
76
  required_rubygems_version: !ruby/object:Gem::Requirement
91
- none: false
92
77
  requirements:
93
- - - ! '>='
78
+ - - '>='
94
79
  - !ruby/object:Gem::Version
95
80
  version: '0'
96
81
  requirements: []
97
82
  rubyforge_project:
98
- rubygems_version: 1.8.24
83
+ rubygems_version: 2.0.3
99
84
  signing_key:
100
- specification_version: 3
85
+ specification_version: 4
101
86
  summary: Rubygems plugin system using Gem.find_files.
102
87
  test_files:
103
- - test/test_paginator.rb
88
+ - test/autodetect_test.rb
89
+ - test/group_test.rb
90
+ - test/name_converter_test.rb
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use ruby-1.9.3-p327@pluginator --create
@@ -1,69 +0,0 @@
1
- require 'pluginator'
2
-
3
- class Pluginator
4
- class UnknownPlugin < StandardError
5
- def initialize(plugin_type, handler)
6
- super "Unknown #{plugin_type}: #{handler}"
7
- end
8
- end
9
-
10
- module ExtraNames
11
- def last_name
12
- self.name.split(/::/).last
13
- end
14
- def last_name_to_sym
15
- class2file(last_name).to_sym
16
- end
17
- def first_name
18
- self.name.split(/::/).first
19
- end
20
- def first_name_to_sym
21
- class2file(first_name).to_sym
22
- end
23
- def class2file(klass)
24
- klass.gsub(/([A-Z])/){|x| "_#{x.downcase}"}[1..-1]
25
- end
26
- end
27
-
28
- class PluginHandler
29
- extend ExtraNames
30
-
31
- class Abstract
32
- extend ExtraNames
33
- def self.handles
34
- last_name_to_sym
35
- end
36
- end
37
-
38
- class << self
39
- attr_accessor :plugin_prefix
40
- attr_accessor :plugin_type
41
- end
42
-
43
- def self.first(handler)
44
- result = plugins[@plugin_type].detect{ |_,plugin| plugin.handles == handler }
45
- result = result.last unless result.nil?
46
- result
47
- end
48
-
49
- def self.first!(handler)
50
- found = first(handler)
51
- raise UnknownPlugin.new(@plugin_type, handler) if found.nil?
52
- found
53
- end
54
-
55
- def self.register_class(klass)
56
- plugins.register_plugin_class(@plugin_type, klass)
57
- end
58
-
59
- def self.plugins
60
- @plugin_prefix ||= first_name_to_sym
61
- @plugin_type ||= last_name_to_sym
62
- @plugins ||= Pluginator.new(@plugin_prefix)
63
- end
64
-
65
- def self.extend_abstract(&block)
66
- self.const_get(:Abstract).class_eval(&block)
67
- end
68
- end
69
- end
File without changes