Module: AroundTheWorld::ClassMethods

Defined in:
lib/around_the_world.rb

Instance Method Summary collapse

Instance Method Details

#around_method(method_name, prevent_double_wrapping_for: nil, &block) ⇒ Object (protected)

Defines a method that gets called around the given instance method.

Examples:

class SomeClass
  around_method :dont_look_in_here do |*args|
    things_happened = super(*args)

    if things_happened
      "Something happened!"
    else
      "Nothing to see here..."
    end
  end

  def dont_look_in_here
    do_some_things
  end
end

SomeClass.new.dont_look_in_here
=> "Something happened!"
around_method :dont_look_in_here, prevent_double_wrapping_for: :memoization do |*args|
  @memoized ||= super(*args)
end

around_method :dont_look_in_here, prevent_double_wrapping_for: :memoization do |*args|
  @memoized ||= super(*args)
end
# => AroundTheWorld::DoubleWrapError:
       "Module AroundTheWorld:ProxyModule:memoization already defines the method :dont_look_in_here"

around_method :dont_look_in_here do |*args|
  do_something_else
  super(*args)
end
# => no error raised
class SomeClass
  include AroundTheWorld

  class << self
    def a_singleton_method; end

    around_method :a_singleton_method do |*args|
      super(*args)
      "It works for class methods too!"
    end
  end
end

SomeClass.a_singleton_method
=> "It works for class methods too!"

Parameters:

  • method_name (Symbol)
  • :prevent_double_wrapping_for (Object)

    If defined, this prevents wrapping the method twice for a given purpose. Accepts any argument.



81
82
83
84
85
86
87
88
# File 'lib/around_the_world.rb', line 81

def around_method(method_name, prevent_double_wrapping_for: nil, &block)
  MethodWrapper.wrap(
    method_name: method_name,
    target: self,
    prevent_double_wrapping_for: prevent_double_wrapping_for,
    &block
  )
end