Module: Invisible

Defined in:
lib/invisible.rb,
lib/invisible/version.rb

Constant Summary collapse

VERSION =
"0.2.2"

Instance Method Summary collapse

Instance Method Details

#append_features(base) ⇒ Object

Extend any module with Invisible and any methods the module overrides will maintain their original visibility.

Examples:

With include

class Base
  def public_method
    'public'
  end

  protected

  def protected_method
    'protected'
  end

  private

  def private_method
    'private'
  end
end

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

class MyClass < Base
  include WithFoo
end

instance = MyClass.new

MyClass.public_method_defined?(:public_method)       #=> true
instance.public_method                               #=> 'publicfoo'

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

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

With prepend

Base.prepend WithFoo

instance = Base.new

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


70
71
72
# File 'lib/invisible.rb', line 70

def append_features(base)
  sync_visibility(base) { super }
end

#prepend_features(base) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/invisible.rb', line 74

def prepend_features(base)
  return super if invisible

  sync_visibility(base, mod = dup)
  mod.invisible = true
  base.const_set("Invisible#{name}", mod) if base.name && name
  base.prepend mod
end