Module: Overridable

Defined in:
lib/overridable.rb

Overview

Including this module in your class will give your class the ability to make its methods to be overridable by included modules. Let’s look at some examples for easier understanding. You are not just limited to write a brandnew method, but also call the original method by ‘super`. Thanks to this feature, you don’t need method chains any more! ;)

You can also use Overridable::ModuleMixin to make things even a bit easier for some situations. See Overridable::ModuleMixin for details.

Examples:

class Thing
  def foo
    puts 'Thing.foo'
  end
end

module Foo
  def foo
    puts 'Foo.foo'
  end
end

# If you don't use Overridable, you will get:
Thing.send :include, Foo
Thing.new.foo #=> print: Thing.foo\n

# If you use Overridable *before* you include the module, things will become to be:
Thing.class_eval {
  include Overridable
  overrides :foo # specifies which methods can be overrided.
  include Foo
}
Thing.new.foo => print: Foo.foo\n
# Let's change the Foo module in the previous example:
module Foo
  def foo
    super
    puts 'Foo.foo'
  end
end

Thing.new.foo #=> print: Thing.foo\nFoo.foo\n

Defined Under Namespace

Modules: ClassMethods, ModuleMixin

Class Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

:nodoc:



135
136
137
# File 'lib/overridable.rb', line 135

def self.included mod #:nodoc:
  mod.extend ClassMethods
end