Class: Plugins::AroundStack::AroundWrapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack, subject, &block) ⇒ AroundWrapper

Returns a new instance of AroundWrapper.



47
48
49
50
51
# File 'lib/plugins/around_stack.rb', line 47

def initialize(stack, subject, &block)
  @stack = stack
  @subject = subject
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



57
58
59
60
# File 'lib/plugins/around_stack.rb', line 57

def method_missing(meth, *args, &block)
  super unless scope
  scope.send(meth, *args, &block)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



45
46
47
# File 'lib/plugins/around_stack.rb', line 45

def block
  @block
end

#stackObject (readonly)

Returns the value of attribute stack.



45
46
47
# File 'lib/plugins/around_stack.rb', line 45

def stack
  @stack
end

#subjectObject (readonly)

Returns the value of attribute subject.



45
46
47
# File 'lib/plugins/around_stack.rb', line 45

def subject
  @subject
end

Instance Method Details

#call(call_stack, *args) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/plugins/around_stack.rb', line 62

def call(call_stack, *args)
  wrapped = call_stack.pop
  raise Errors::Error, 'call stack too short' unless wrapped

  define_singleton_method("perform_#{subject}") { |*new_args|
    args = new_args unless new_args.empty?
    wrapped.call(call_stack, *args)
  }

  singleton_class.instance_exec(subject) { |subject|
    alias_method subject, "perform_#{subject}"

    # Don't allow these to be overriden
    attr_reader :stack, :subject, :block
  }

  instance_exec(*args, &block)
end

#scopeObject



53
54
55
# File 'lib/plugins/around_stack.rb', line 53

def scope
  stack.scope
end