Module: AroundTheWorld::ClassMethods

Defined in:
lib/around_the_world.rb

Instance Method Summary collapse

Instance Method Details

#around_method(*method_names, prevent_double_wrapping_for: nil, allow_undefined_method: false, &block) ⇒ Object (protected)

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!"

Examples:

around_method :dont_look_in_here, prevent_double_wrapping_for: :memoization do |...|
  @memoized ||= super(...)
end

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

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

  class << self
    def a_singleton_method; end

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

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

Parameters:

  • method_names (Array<Symbol, String>)

    A list of methods to be wrapped

  • :prevent_double_wrapping_for (Object)

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

  • :allow_undefined_method (Boolean)

    When false, an error is raised if the wrapped method is not explicitly defined by the target module or class. Default: false



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/around_the_world.rb', line 84

def around_method(*method_names, prevent_double_wrapping_for: nil, allow_undefined_method: false, &block)
  method_names.each do |method_name|
    MethodWrapper.wrap(
      method_name: method_name,
      target: self,
      prevent_double_wrapping_for: prevent_double_wrapping_for,
      allow_undefined_method: allow_undefined_method,
      &block
    )
  end
end