ribbon-plugins 0.2.2 → 0.2.3

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: 46af700c614f85fd5bdcba3d978ff0a5d1432b04
4
- data.tar.gz: 31dac96000c321fa54cb8959a875dc6d6ae20c0f
3
+ metadata.gz: 824abb9cd85dbdc1f999f5ec813289e718e9eb91
4
+ data.tar.gz: c4b52869f401bf9e3384593da9265432e3af40e0
5
5
  SHA512:
6
- metadata.gz: 9488797076e7bcf2fd6ec6744c4d402ca0f405ab8a3c0e97eae0dc847ac8549ff4dab185f529c16b16cebdc9036296e35a8156df55b5f6a8173cc882f37d005f
7
- data.tar.gz: fa203501f477a8326f2169a3138c2f60153f9e92d5dda8bf09ab2a8bf046fd29c0ae0d54eec541186e034a8c6c489c32448a357d85f7c15b65c2602cb602b173
6
+ metadata.gz: 8311911a1dd18b73c1e24515ae2accdde4010cd94b5046f35f9e017b0c2baa59824591923975a7e78de972a657f986a49fb311f7e3c279ae15632b75cad3fb44
7
+ data.tar.gz: 2dd783003780802f2493fb31685960fc750ee0fd7a92f5a29821843c32661bfc86495f1458fa4f12d530d96a98dbcb97f7a4fcde13c68895fa3997032718fd65
@@ -1,20 +1,30 @@
1
1
  class Ribbon::Plugins
2
2
  class AroundStack
3
3
  attr_reader :subject
4
+ attr_accessor :scope
4
5
 
5
- def initialize(subject)
6
+ def initialize(subject, scope=nil)
6
7
  @subject = subject.to_sym
8
+ @scope = scope
7
9
  @_stack = []
8
10
  end
9
11
 
10
12
  def push(&block)
11
13
  raise Errors::Error, "Must pass block" unless block_given?
12
14
 
13
- AroundWrapper.new(subject, &block).tap { |wrapper|
15
+ AroundWrapper.new(self, subject, &block).tap { |wrapper|
14
16
  @_stack.push(wrapper)
15
17
  }
16
18
  end
17
19
 
20
+ def dup
21
+ AroundStack.new(subject, scope).tap { |stack|
22
+ @_stack.each { |wrapper|
23
+ stack.push(&wrapper.block)
24
+ }
25
+ }
26
+ end
27
+
18
28
  def call(*args, &block)
19
29
  raise Errors::Error, "Must pass block" unless block_given?
20
30
  call_stack = @_stack.dup
@@ -32,13 +42,23 @@ class Ribbon::Plugins
32
42
  end
33
43
 
34
44
  class AroundWrapper
35
- attr_reader :subject, :block
45
+ attr_reader :stack, :subject, :block
36
46
 
37
- def initialize(subject, &block)
47
+ def initialize(stack, subject, &block)
48
+ @stack = stack
38
49
  @subject = subject
39
50
  @block = block
40
51
  end
41
52
 
53
+ def scope
54
+ stack.scope
55
+ end
56
+
57
+ def method_missing(meth, *args, &block)
58
+ super unless scope
59
+ scope.send(meth, *args, &block)
60
+ end
61
+
42
62
  def call(call_stack, *args)
43
63
  wrapped = call_stack.pop
44
64
  raise Errors::Error, 'call stack too short' unless wrapped
@@ -48,6 +68,13 @@ class Ribbon::Plugins
48
68
  wrapped.call(call_stack, *args)
49
69
  }
50
70
 
71
+ singleton_class.instance_exec(subject) { |subject|
72
+ alias_method subject, "perform_#{subject}"
73
+
74
+ # Don't allow these to be overriden
75
+ attr_reader :stack, :subject, :block
76
+ }
77
+
51
78
  instance_exec(*args, &block)
52
79
  end
53
80
  end
@@ -64,6 +91,8 @@ class Ribbon::Plugins
64
91
  !!@_called
65
92
  end
66
93
 
94
+ ##
95
+ # Call the wrapped block, ignoring the scope and call_stack arguments.
67
96
  def call(call_stack, *args)
68
97
  raise Errors::Error, 'receiving non-empty call stack' unless call_stack.empty?
69
98
  block.call(*args).tap { |retval|
@@ -1,15 +1,30 @@
1
1
  class Ribbon::Plugins
2
2
  class BlockStack
3
- def initialize
3
+ attr_accessor :scope
4
+
5
+ def initialize(scope=nil)
6
+ @scope = scope
4
7
  @_stack = []
5
8
  end
6
9
 
10
+ def dup
11
+ BlockStack.new.tap { |stack|
12
+ @_stack.each { |block| stack.push(&block) }
13
+ }
14
+ end
15
+
7
16
  def push(&block)
8
17
  @_stack.push(block)
9
18
  end
10
19
 
11
20
  def call(*args)
12
- @_stack.reverse_each { |block| block.call(*args) }
21
+ @_stack.reverse_each { |block|
22
+ if scope
23
+ scope.instance_exec(*args, &block)
24
+ else
25
+ block.call(*args)
26
+ end
27
+ }
13
28
  end
14
29
  end
15
30
  end
@@ -39,8 +39,8 @@ module Ribbon
39
39
 
40
40
  ##
41
41
  # Add a plugin.
42
- def plugin(plugin)
43
- plugins.add(plugin)
42
+ def plugin(*args, &block)
43
+ plugins.add(*args, &block)
44
44
  end
45
45
  end
46
46
  end
@@ -53,7 +53,10 @@ class Ribbon::Plugins
53
53
 
54
54
  private
55
55
  def _callbacks(type, subject)
56
- self.class._callbacks(type, subject)
56
+ ((@_callbacks ||= {})[type.to_sym] ||= {})[subject.to_sym] ||=
57
+ self.class._callbacks(type, subject).dup.tap { |stack|
58
+ stack.scope = self
59
+ }
57
60
  end
58
61
  end # Plugin
59
62
  end # Ribbon::Plugins
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  class Plugins
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
@@ -15,9 +15,9 @@ module Ribbon
15
15
  @plugin_loader = block
16
16
  end
17
17
 
18
- def add(plugin=nil, &block)
18
+ def add(plugin=nil, *args, &block)
19
19
  plugin = _load(plugin, &block)
20
- _add_plugin(plugin.new(self))
20
+ _add_plugin(plugin.new(self, *args))
21
21
  end
22
22
 
23
23
  def clear
@@ -51,7 +51,7 @@ module Ribbon
51
51
  end
52
52
 
53
53
  def _around_stack
54
- @__around_stack ||= AroundStack.new(:block)
54
+ @__around_stack ||= AroundStack.new(:block, self)
55
55
  end
56
56
 
57
57
  def _add_plugin(plugin)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer