Class: Plugins::Plugin

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins = nil) ⇒ Plugin

Returns a new instance of Plugin.



38
39
40
# File 'lib/plugins/plugin.rb', line 38

def initialize(plugins=nil)
  @plugins = plugins
end

Instance Attribute Details

#pluginsObject (readonly)

Returns the value of attribute plugins.



36
37
38
# File 'lib/plugins/plugin.rb', line 36

def plugins
  @plugins
end

Class Method Details

._callbacks(type, subject) ⇒ Object



20
21
22
# File 'lib/plugins/plugin.rb', line 20

def _callbacks(type, subject)
  ((@__callbacks ||= {})[type.to_sym] ||= {})[subject.to_sym] ||= _empty_stack_for(type, subject)
end

._define_callback(type, subject, &block) ⇒ Object



16
17
18
# File 'lib/plugins/plugin.rb', line 16

def _define_callback(type, subject, &block)
  _callbacks(type, subject).push(&block)
end

._empty_stack_for(type, subject) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/plugins/plugin.rb', line 24

def _empty_stack_for(type, subject)
  case type.to_sym
  when :before, :after
    BlockStack.new
  when :around
    AroundStack.new(subject)
  else
    raise Errors::Error, "Invalid type: #{type}"
  end
end

.create(&block) ⇒ Object



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

def create(&block)
  Class.new(Plugin).tap { |k| k.class_eval(&block) if block }
end

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



8
9
10
11
12
13
14
# File 'lib/plugins/plugin.rb', line 8

def method_missing(meth, *args, &block)
  if /^(before|after|around)_(\w+)$/.match(meth.to_s)
    _define_callback($1, $2, &block)
  else
    super
  end
end

Instance Method Details

#after(subject, *args) ⇒ Object



46
47
48
# File 'lib/plugins/plugin.rb', line 46

def after(subject, *args)
  _callbacks(:after, subject).call(*args)
end

#around(subject, *args, &block) ⇒ Object



50
51
52
# File 'lib/plugins/plugin.rb', line 50

def around(subject, *args, &block)
  _callbacks(:around, subject).call(*args, &block)
end

#before(subject, *args) ⇒ Object



42
43
44
# File 'lib/plugins/plugin.rb', line 42

def before(subject, *args)
  _callbacks(:before, subject).call(*args)
end