Module: Mechanize::Prependable

Defined in:
lib/mechanize/prependable.rb

Overview

Fake implementation of prepend(), which does not support overriding inherited methods nor methods that are formerly overridden by another invocation of prepend().

Here’s what <Original>.prepend(<Wrapper>) does:

  • Create an anonymous stub module (hereinafter <Stub>) and define <Stub>#<method> that calls #<method>without<Wrapper> for each instance method of <Wrapper>.

  • Rename <Original>#<method> to #<method>without<Wrapper> for each instance method of <Wrapper>.

  • Include <Wrapper> and <Stub> into <Original> in that order.

This way, a call of <Original>#<method> is dispatched to <Wrapper><method>, which may call super which is dispatched to <Stub>#<method>, which finally calls <Original>#<method>without<Wrapper> which is used to be called <Original>#<method>.

Usage:

class Mechanize
  # module with methods that overrides those of X
  module Y
  end

  unless X.respond_to?(:prepend, true)
    require 'mechanize/prependable'
    X.extend(Prependable)
  end

  class X
    prepend Y
  end
end