Module: Before::ClassMethods

Defined in:
lib/base/simple_aop.rb

Constant Summary collapse

PREFIX =
"___".freeze

Instance Method Summary collapse

Instance Method Details

#before(methods, callbacks) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/base/simple_aop.rb', line 42

def before(methods, callbacks)
  Array(methods).each do | method|
    method = method.to_sym
    callbacks = Array(callbacks).map{|callback| callback.to_sym}

    enhance_method(method, callbacks)
  end
end

#enhance_method(method, callbacks) ⇒ Object

enhance single method with callbacks



52
53
54
55
56
57
58
59
60
61
# File 'lib/base/simple_aop.rb', line 52

def enhance_method(method, callbacks)
  _method = (PREFIX + method.to_s).to_sym
  alias_method _method, method

  self.send(:define_method, method) do |*args, &blk|
    [callbacks, _method].flatten.each do |callback|
      break unless self.send(callback, *args, &blk)
    end
  end
end