ribbon-plugins 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 230fbea0e698049dd095f097a6d977eca0e17359
4
- data.tar.gz: 65c29190976d2a231e41493ad9840dbf9bdf90f3
3
+ metadata.gz: b1203cf7726507ab9760f6ba519ea768fcbf5c19
4
+ data.tar.gz: 93210e25c99e2d1381c64baff34375e9d41c9f4f
5
5
  SHA512:
6
- metadata.gz: 6635638f54be4b62c15e4e988f60636d2e37d2bbb0d8686f785e2cba13a11a994d7f7a51dc664085f8cd021de9a2920a66c6a9531276b1c188cd9e7979537e8f
7
- data.tar.gz: c70c4dec948b31c09c49e8bf61be462386095da0bce1ae5d3032b7951a46f77ec660932a3fd0a0bc7f6477f432f4c3b8b1bb0b446302477aa62bf5584298064b
6
+ metadata.gz: a016c927a8a9061452311957494d201bc6ffb220c68582358b048626171baa0042f38d2841e1f46b37c169aa2f28209a502d4b32ad837ffb95e4556b9971a2ee
7
+ data.tar.gz: 2eaf238f288063cf187e7c6f0454b4a1ce2dddfb6dec76c2d1def45b0acafd3c65a4978beac6704cb317f6f01ce977d693329bdab89c73f413c8ed4e34aafe83
@@ -0,0 +1,47 @@
1
+ module Ribbon
2
+ class Plugins
3
+ ##
4
+ # Intended to be mixed into any class utilizing the plugins functionality.
5
+ module ComponentMixin
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ ##
12
+ # Get or define the plugin loader.
13
+ #
14
+ # This block will be used to load a plugin given the value passed to the
15
+ # +plugin+ instance method. It's the responsibility of this block to
16
+ # translate the inputted value into either a Class that extends Plugin
17
+ # or a Proc.
18
+ #
19
+ # If for a particular value you wish to not perform any translation,
20
+ # return falsey.
21
+ def plugin_loader(&block)
22
+ if block_given?
23
+ @_plugin_loader = block
24
+ else
25
+ @_plugin_loader
26
+ end
27
+ end
28
+ end
29
+
30
+ ###
31
+ # Instance Methods
32
+ ###
33
+
34
+ ##
35
+ # Reference to the Plugins instance for the component.
36
+ def plugins
37
+ @plugins ||= Plugins.new(self, &self.class.plugin_loader)
38
+ end
39
+
40
+ ##
41
+ # Add a plugin.
42
+ def plugin(plugin)
43
+ plugins.add(plugin)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -33,6 +33,12 @@ class Ribbon::Plugins
33
33
  end
34
34
  end
35
35
 
36
+ attr_reader :plugins
37
+
38
+ def initialize(plugins=nil)
39
+ @plugins = plugins
40
+ end
41
+
36
42
  def before(subject, *args)
37
43
  _callbacks(:before, subject).call(*args)
38
44
  end
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  class Plugins
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,18 +2,22 @@ require 'ribbon/plugins/version'
2
2
 
3
3
  module Ribbon
4
4
  class Plugins
5
- autoload(:Errors, 'ribbon/plugins/errors')
6
- autoload(:Plugin, 'ribbon/plugins/plugin')
7
- autoload(:AroundStack, 'ribbon/plugins/around_stack')
8
- autoload(:BlockStack, 'ribbon/plugins/block_stack')
5
+ autoload(:Errors, 'ribbon/plugins/errors')
6
+ autoload(:Plugin, 'ribbon/plugins/plugin')
7
+ autoload(:AroundStack, 'ribbon/plugins/around_stack')
8
+ autoload(:BlockStack, 'ribbon/plugins/block_stack')
9
+ autoload(:ComponentMixin, 'ribbon/plugins/component_mixin')
9
10
 
10
- def initialize(plugin_module=nil)
11
- @_plugin_module = plugin_module
11
+ attr_reader :component, :plugin_loader
12
+
13
+ def initialize(component=nil, &block)
14
+ @component = component
15
+ @plugin_loader = block
12
16
  end
13
17
 
14
18
  def add(plugin=nil, &block)
15
19
  plugin = _load(plugin, &block)
16
- _add_plugin(plugin.new)
20
+ _add_plugin(plugin.new(self))
17
21
  end
18
22
 
19
23
  def clear
@@ -50,11 +54,14 @@ module Ribbon
50
54
 
51
55
  def _add_plugin(plugin)
52
56
  _plugins.push(plugin)
57
+
53
58
  _around_stack.push { |subject, *args|
54
59
  plugin.around(subject, *args) {
55
60
  perform_block
56
61
  }
57
62
  }
63
+
64
+ plugin
58
65
  end
59
66
 
60
67
  def _load(plugin, &block)
@@ -68,12 +75,12 @@ module Ribbon
68
75
  end
69
76
 
70
77
  def _load_plugin(plugin)
78
+ _call_plugin_loader(plugin).tap { |p| plugin = p if p }
79
+
71
80
  case plugin
72
81
  when Class
73
82
  plugin < Plugin && plugin or
74
83
  raise Errors::LoadError, "Invalid plugin class: #{plugin.inspect} Must extend Plugin."
75
- when Symbol, String
76
- _load_plugin_by_name(plugin)
77
84
  when Proc
78
85
  Plugin.create(&plugin)
79
86
  else
@@ -81,9 +88,8 @@ module Ribbon
81
88
  end
82
89
  end
83
90
 
84
- def _load_plugin_by_name(name)
85
- @_plugin_module && @_plugin_module.const_get("#{name.upcase}Plugin") or
86
- raise Errors::LoadError, "Cannot load plugin by name. No plugin module provided."
91
+ def _call_plugin_loader(plugin)
92
+ plugin_loader && component.instance_exec(plugin, &plugin_loader)
87
93
  end
88
94
  end # Plugins
89
95
  end # Ribbon
metadata CHANGED
@@ -1,33 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.2'
20
- - - '>='
20
+ - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 3.2.0
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.2'
30
- - - '>='
30
+ - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.2.0
33
33
  description: A flexible plugins framework.
@@ -40,6 +40,7 @@ files:
40
40
  - lib/ribbon/plugins.rb
41
41
  - lib/ribbon/plugins/around_stack.rb
42
42
  - lib/ribbon/plugins/block_stack.rb
43
+ - lib/ribbon/plugins/component_mixin.rb
43
44
  - lib/ribbon/plugins/errors.rb
44
45
  - lib/ribbon/plugins/plugin.rb
45
46
  - lib/ribbon/plugins/version.rb
@@ -53,17 +54,17 @@ require_paths:
53
54
  - lib
54
55
  required_ruby_version: !ruby/object:Gem::Requirement
55
56
  requirements:
56
- - - '>='
57
+ - - ">="
57
58
  - !ruby/object:Gem::Version
58
59
  version: '0'
59
60
  required_rubygems_version: !ruby/object:Gem::Requirement
60
61
  requirements:
61
- - - '>='
62
+ - - ">="
62
63
  - !ruby/object:Gem::Version
63
64
  version: '0'
64
65
  requirements: []
65
66
  rubyforge_project:
66
- rubygems_version: 2.2.2
67
+ rubygems_version: 2.4.5
67
68
  signing_key:
68
69
  specification_version: 4
69
70
  summary: A flexible plugins framework.