module Omniperm::CallbackDecorators::MethodsDecorator

Public Instance Methods

__after_method() click to toggle source
# File lib/omniperm/decorators.rb, line 9
def __after_method
end
__before_method() click to toggle source
# File lib/omniperm/decorators.rb, line 6
def __before_method
end
decorate_methods_with_before_and_after() click to toggle source
# File lib/omniperm/decorators.rb, line 12
def decorate_methods_with_before_and_after
  target_methods = singleton_methods(false) + instance_methods(false)
  target_methods.reject{|value| [:inject_before_to_methods, :__before_method, :__after_method].include?(value) }.each { |m|
    # Rename original method
    target = nil
    target = self if instance_methods(false).include?(m.to_sym) # decorate instance methods
    # target = self.singleton_class if singleton_methods(false).include?(m.to_sym) # decorate class methods
    if target
      target.send(:alias_method, "__#{m}_original", m)
      target.define_method m do |*args, **kwargs|
        before_method = Proc.new { 
          if respond_to?("__before_method")
            return_value = __before_method
          else
            return_value = target.__before_method
          end
          
          return return_value unless return_value.nil? 
        };
        before_method.call

        if kwargs.empty?
          return self.send "__#{m}_original", *args 
        else
          return self.send "__#{m}_original", *args, **kwargs
        end
        # after_method = Proc.new {
        #   return_value = __after_method
        #   return return_value unless return_value.nil?
        # };
        # after_method.call
      end
    end
  }
end