Class: Plugins::AroundStack

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/around_stack.rb

Defined Under Namespace

Classes: AroundWrapper, WrappedBlock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, scope = nil) ⇒ AroundStack

Returns a new instance of AroundStack.



6
7
8
9
10
# File 'lib/plugins/around_stack.rb', line 6

def initialize(subject, scope=nil)
  @subject = subject.to_sym
  @scope = scope
  @_stack = []
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



4
5
6
# File 'lib/plugins/around_stack.rb', line 4

def scope
  @scope
end

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/plugins/around_stack.rb', line 3

def subject
  @subject
end

Instance Method Details

#call(*args, &block) ⇒ Object

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plugins/around_stack.rb', line 28

def call(*args, &block)
  raise Errors::Error, "Must pass block" unless block_given?
  call_stack = @_stack.dup

  inner_most = WrappedBlock.new(&block)
  call_stack.unshift(inner_most)

  outer_most = call_stack.pop
  outer_most.call(call_stack, *args)

  # This shouldn't happen unless the AroundStack isn't functioning properly.
  raise Errors::Error, "Block passed was not called!" unless inner_most.called?

  inner_most.retval
end

#dupObject



20
21
22
23
24
25
26
# File 'lib/plugins/around_stack.rb', line 20

def dup
  AroundStack.new(subject, scope).tap { |stack|
    @_stack.each { |wrapper|
      stack.push(&wrapper.block)
    }
  }
end

#push(&block) ⇒ Object

Raises:



12
13
14
15
16
17
18
# File 'lib/plugins/around_stack.rb', line 12

def push(&block)
  raise Errors::Error, "Must pass block" unless block_given?

  AroundWrapper.new(self, subject, &block).tap { |wrapper|
    @_stack.push(wrapper)
  }
end