plugins 0.2.4 → 0.3.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: 95e5717b6ed8f94cb9b10852dd0ffe859694411c
4
- data.tar.gz: 38b115995d982f05f361b66724af2b7b24e4f514
3
+ metadata.gz: 456787abd50681a78ad147e4a211cb48def2d30f
4
+ data.tar.gz: 8a4ac2ed935ef744f9848c2d9537edab96f608e3
5
5
  SHA512:
6
- metadata.gz: 96d3f53560dcb1f37190b5ac7bd093de98b164a7238ea09cdd5cc5eae98bcb3d53aff42d72aa9c4f1f83b35452ae68d1fc8ac604797f40f9b090d5e02e064708
7
- data.tar.gz: 14322d806fa42ab7f954b5393eeffc5a6a5fcfaddf3fd7bb256b326051a1d001e926ea149a2b17c49ecea617feea711ba351f5b5c6889c43e74ad798c0096254
6
+ metadata.gz: 3f9059010da8fb8b9234bf989b40803d236b462ddbddd46453f56190bc5eecb0662d1b007badbb707c70b269aa36123541574125aaf437686ed9f0113e8060bb
7
+ data.tar.gz: 98621f1f7ca9c6f0565261fd9250a753329d45edd58399a355f82f3967c051ed87b7a0dd6638d27dc40c080d8180a37f8050255f2d447b228ecf63979dfbb1c6
@@ -0,0 +1,95 @@
1
+ require 'plugins/version'
2
+
3
+ class Plugins
4
+ autoload(:Errors, 'plugins/errors')
5
+ autoload(:Plugin, 'plugins/plugin')
6
+ autoload(:AroundStack, 'plugins/around_stack')
7
+ autoload(:BlockStack, 'plugins/block_stack')
8
+ autoload(:ComponentMixin, 'plugins/component_mixin')
9
+
10
+ attr_reader :component, :plugin_loader
11
+
12
+ def initialize(component=nil, &block)
13
+ @component = component
14
+ @plugin_loader = block
15
+ end
16
+
17
+ def add(plugin=nil, *args, &block)
18
+ plugin = _load(plugin, &block)
19
+ _add_plugin(plugin.new(self, *args))
20
+ end
21
+
22
+ def clear
23
+ @_plugins = nil
24
+ @_around_stack = nil
25
+ end
26
+
27
+ def before(subject, *args)
28
+ _plugins.reverse_each { |plugin| plugin.before(subject, *args) }
29
+ end
30
+
31
+ def after(subject, *args)
32
+ _plugins.reverse_each { |plugin| plugin.after(subject, *args) }
33
+ end
34
+
35
+ def around(subject, *args, &block)
36
+ _around_stack.call(subject, *args) { |subject, *args| block.call(*args) }
37
+ end
38
+
39
+ def perform(subject, *args, &block)
40
+ before(subject, *args)
41
+ retval = around(subject, *args, &block)
42
+ after(subject, *args)
43
+
44
+ retval
45
+ end
46
+
47
+ private
48
+ def _plugins
49
+ @_plugins ||= []
50
+ end
51
+
52
+ def _around_stack
53
+ @_around_stack ||= AroundStack.new(:block, self)
54
+ end
55
+
56
+ def _add_plugin(plugin)
57
+ _plugins.push(plugin)
58
+
59
+ _around_stack.push { |subject, *args|
60
+ plugin.around(subject, *args) { |*args|
61
+ perform_block(subject, *args)
62
+ }
63
+ }
64
+
65
+ plugin
66
+ end
67
+
68
+ def _load(plugin, &block)
69
+ if plugin
70
+ _load_plugin(plugin)
71
+ elsif block_given?
72
+ Plugin.create(&block)
73
+ else
74
+ raise Errors::LoadError, 'No plugin information provided'
75
+ end
76
+ end
77
+
78
+ def _load_plugin(plugin)
79
+ _call_plugin_loader(plugin).tap { |p| plugin = p if p }
80
+
81
+ case plugin
82
+ when Class
83
+ plugin < Plugin && plugin or
84
+ raise Errors::LoadError, "Invalid plugin class: #{plugin.inspect} Must extend Plugin."
85
+ when Proc
86
+ Plugin.create(&plugin)
87
+ else
88
+ raise Errors::LoadError, "Invalid plugin identifier: #{plugin.inspect}"
89
+ end
90
+ end
91
+
92
+ def _call_plugin_loader(plugin)
93
+ plugin_loader && component.instance_exec(plugin, &plugin_loader)
94
+ end
95
+ end # Plugins
@@ -1,4 +1,4 @@
1
- class Ribbon::Plugins
1
+ class Plugins
2
2
  class AroundStack
3
3
  attr_reader :subject
4
4
  attr_accessor :scope
@@ -102,4 +102,4 @@ class Ribbon::Plugins
102
102
  end
103
103
  end
104
104
  end # AroundStack
105
- end # Ribbon::Plugins
105
+ end # Plugins
@@ -1,4 +1,4 @@
1
- class Ribbon::Plugins
1
+ class Plugins
2
2
  class BlockStack
3
3
  attr_accessor :scope
4
4
 
@@ -27,4 +27,4 @@ class Ribbon::Plugins
27
27
  }
28
28
  end
29
29
  end
30
- end
30
+ end
@@ -0,0 +1,45 @@
1
+ class Plugins
2
+ ##
3
+ # Intended to be mixed into any class utilizing the plugins functionality.
4
+ module ComponentMixin
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ ##
11
+ # Get or define the plugin loader.
12
+ #
13
+ # This block will be used to load a plugin given the value passed to the
14
+ # +plugin+ instance method. It's the responsibility of this block to
15
+ # translate the inputted value into either a Class that extends Plugin
16
+ # or a Proc.
17
+ #
18
+ # If for a particular value you wish to not perform any translation,
19
+ # return falsey.
20
+ def plugin_loader(&block)
21
+ if block_given?
22
+ @_plugin_loader = block
23
+ else
24
+ @_plugin_loader
25
+ end
26
+ end
27
+ end
28
+
29
+ ###
30
+ # Instance Methods
31
+ ###
32
+
33
+ ##
34
+ # Reference to the Plugins instance for the component.
35
+ def plugins
36
+ @plugins ||= Plugins.new(self, &self.class.plugin_loader)
37
+ end
38
+
39
+ ##
40
+ # Add a plugin.
41
+ def plugin(*args, &block)
42
+ plugins.add(*args, &block)
43
+ end
44
+ end
45
+ end
@@ -1,6 +1,6 @@
1
- class Ribbon::Plugins
1
+ class Plugins
2
2
  module Errors
3
3
  class Error < StandardError; end
4
4
  class LoadError < Error; end
5
5
  end
6
- end
6
+ end
@@ -1,4 +1,4 @@
1
- class Ribbon::Plugins
1
+ class Plugins
2
2
  class Plugin
3
3
  class << self
4
4
  def create(&block)
@@ -59,4 +59,4 @@ class Ribbon::Plugins
59
59
  }
60
60
  end
61
61
  end # Plugin
62
- end # Ribbon::Plugins
62
+ end # Plugins
@@ -0,0 +1,3 @@
1
+ class Plugins
2
+ VERSION = "0.3.0"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
@@ -32,19 +32,19 @@ dependencies:
32
32
  version: 3.2.0
33
33
  description: A flexible plugins framework.
34
34
  email:
35
- - robert@ribbonpayments.com
35
+ - robert@payout.com
36
36
  executables: []
37
37
  extensions: []
38
38
  extra_rdoc_files: []
39
39
  files:
40
- - lib/ribbon/plugins.rb
41
- - lib/ribbon/plugins/around_stack.rb
42
- - lib/ribbon/plugins/block_stack.rb
43
- - lib/ribbon/plugins/component_mixin.rb
44
- - lib/ribbon/plugins/errors.rb
45
- - lib/ribbon/plugins/plugin.rb
46
- - lib/ribbon/plugins/version.rb
47
- homepage: http://github.com/ribbon/plugins
40
+ - lib/plugins.rb
41
+ - lib/plugins/around_stack.rb
42
+ - lib/plugins/block_stack.rb
43
+ - lib/plugins/component_mixin.rb
44
+ - lib/plugins/errors.rb
45
+ - lib/plugins/plugin.rb
46
+ - lib/plugins/version.rb
47
+ homepage: http://github.com/payout/plugins
48
48
  licenses:
49
49
  - BSD
50
50
  metadata: {}
@@ -1,97 +0,0 @@
1
- require 'ribbon/plugins/version'
2
-
3
- module Ribbon
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')
9
- autoload(:ComponentMixin, 'ribbon/plugins/component_mixin')
10
-
11
- attr_reader :component, :plugin_loader
12
-
13
- def initialize(component=nil, &block)
14
- @component = component
15
- @plugin_loader = block
16
- end
17
-
18
- def add(plugin=nil, *args, &block)
19
- plugin = _load(plugin, &block)
20
- _add_plugin(plugin.new(self, *args))
21
- end
22
-
23
- def clear
24
- @_plugins = nil
25
- @_around_stack = nil
26
- end
27
-
28
- def before(subject, *args)
29
- _plugins.reverse_each { |plugin| plugin.before(subject, *args) }
30
- end
31
-
32
- def after(subject, *args)
33
- _plugins.reverse_each { |plugin| plugin.after(subject, *args) }
34
- end
35
-
36
- def around(subject, *args, &block)
37
- _around_stack.call(subject, *args) { |subject, *args| block.call(*args) }
38
- end
39
-
40
- def perform(subject, *args, &block)
41
- before(subject, *args)
42
- retval = around(subject, *args, &block)
43
- after(subject, *args)
44
-
45
- retval
46
- end
47
-
48
- private
49
- def _plugins
50
- @_plugins ||= []
51
- end
52
-
53
- def _around_stack
54
- @_around_stack ||= AroundStack.new(:block, self)
55
- end
56
-
57
- def _add_plugin(plugin)
58
- _plugins.push(plugin)
59
-
60
- _around_stack.push { |subject, *args|
61
- plugin.around(subject, *args) { |*args|
62
- perform_block(subject, *args)
63
- }
64
- }
65
-
66
- plugin
67
- end
68
-
69
- def _load(plugin, &block)
70
- if plugin
71
- _load_plugin(plugin)
72
- elsif block_given?
73
- Plugin.create(&block)
74
- else
75
- raise Errors::LoadError, 'No plugin information provided'
76
- end
77
- end
78
-
79
- def _load_plugin(plugin)
80
- _call_plugin_loader(plugin).tap { |p| plugin = p if p }
81
-
82
- case plugin
83
- when Class
84
- plugin < Plugin && plugin or
85
- raise Errors::LoadError, "Invalid plugin class: #{plugin.inspect} Must extend Plugin."
86
- when Proc
87
- Plugin.create(&plugin)
88
- else
89
- raise Errors::LoadError, "Invalid plugin identifier: #{plugin.inspect}"
90
- end
91
- end
92
-
93
- def _call_plugin_loader(plugin)
94
- plugin_loader && component.instance_exec(plugin, &plugin_loader)
95
- end
96
- end # Plugins
97
- end # Ribbon
@@ -1,47 +0,0 @@
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(*args, &block)
43
- plugins.add(*args, &block)
44
- end
45
- end
46
- end
47
- end
@@ -1,5 +0,0 @@
1
- module Ribbon
2
- class Plugins
3
- VERSION = "0.2.4"
4
- end
5
- end