Project

invisible

0.01
No commit activity in last 3 years
No release in over 3 years
Override methods while maintaining their original visibility.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 10.0
~> 3.0
 Project Readme

Invisible

Gem Version Build Status

Public? Private? Protected? Who cares! I just wanna monkey patch that shit!

No fear: Invisible has your back! In a dozen lines of code, this little gem does away with the problem of maintaining original method visibility, so you can get on with your monkey-patching mayhem.

Usage

Suppose you are defining a module which will override a bunch of methods from some class (or module). Simply extend Invisible and you can ignore checking whether those methods are public, protected or private -- Invisible will take care of that for you.

Suppose this is the class we are overriding:

class Base
  def public_method
    'public'
  end

  protected

  def protected_method
    'protected'
  end

  private

  def private_method
    'private'
  end
end

We don't want to care about whether the methods are private or whatever. So we define our module like so:

module WithFoo
  extend Invisible

  def public_method
    super + ' with foo'
  end

  def protected_method
    super + ' with foo'
  end

  def private_method
    super + ' with foo'
  end
end

Normally, without Invisible, we would have just made methods that were previously private or protected become public. But Invisible checks the original visibility and ensures that when the module is included, methods which were originally private or protected stay that way.

class MyClass < Base
  include WithFoo
end

instance = MyClass.new

MyClass.public_method_defined?(:public_method)       #=> true
instance.public_method                               #=> 'public with foo'

MyClass.protected_method_defined?(:protected_method) #=> true
instance.protected_method                            # raises NoMethodError
instance.send(:protected_method)                     #=> 'protected with foo'

MyClass.private_method_defined?(:private_method)     #=> true
instance.private_method                              # raises NoMethodError
instance.send(:private_method)                       #=> 'private with foo'

Also works with prepend:

Base.prepend WithFoo

instance = Base.new

Base.private_method_defined?(:private_method)        # true
instance.private_method                              # raises NoMethodError
instance.send(:private_method)                       #=> 'private with foo'

License

The gem is available as open source under the terms of the MIT License.