class Utter::Utils::Wrapper

Public Class Methods

inherited(klass) click to toggle source
# File lib/utter/utils/wrapper.rb, line 14
def self.inherited(klass)
  def klass.method_added(name)
    # prevent a SystemStackError
    return if @_not_new
    @_not_new = true

    # preserve the original method call
    original = "original #{name}"
    alias_method original, name

    # wrap the method call
    define_method(name) do |*args, &block|
      self.class.superclass.utter_before_action.call(self, args, name)

      # call the original method
      result = send original, *args, &block

      self.class.superclass.utter_after_action.call(self, args, name)

      # return the original return value
      result
    end

    # reset the guard for the next method definition
    @_not_new = false
  end
end

Public Instance Methods

wrap(wrapped_class, before: nil, after: nil) click to toggle source
# File lib/utter/utils/wrapper.rb, line 4
def wrap(wrapped_class, before: nil, after: nil)
  wrapped_class.class_eval do
    self.define_singleton_method :utter_before_action do
      before || Proc.new {}
    end

    self.define_singleton_method :utter_after_action do
      after || Proc.new {}
    end

    def self.inherited(klass)
      def klass.method_added(name)
        # prevent a SystemStackError
        return if @_not_new
        @_not_new = true

        # preserve the original method call
        original = "original #{name}"
        alias_method original, name

        # wrap the method call
        define_method(name) do |*args, &block|
          self.class.superclass.utter_before_action.call(self, args, name)

          # call the original method
          result = send original, *args, &block

          self.class.superclass.utter_after_action.call(self, args, name)

          # return the original return value
          result
        end

        # reset the guard for the next method definition
        @_not_new = false
      end
    end
  end
end