0.0
No commit activity in last 3 years
No release in over 3 years
Ruby macro for defining hook methods to be overriden
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

Purpose

When using any design pattern that requires the definition of hook methods in a parent class (such as the Template Pattern), it can become tedious to define methods that only raise NotImplementedError. Furthermore, as a developer on the project, depending on the size of the parent class from which you are inheriting, it may not be immediately obvious which methods your subclass needs to implemenet.

An example:

# Typical implementation of hook methods in parent classes

class BaseClass

# code omitted

  def method_a
    raise NotImplementedError, "You need to implement this method."
  end

  def method_b
    raise NotImplementedError, "You need to implement this method."
  end

  def method_c
    raise NotImplementedError, "You need to implement this method."
  end
end

template_method solves both of these problems. It provides a hook_methods ( hook_method works as well) macro to reduce boilerplate, and by defining hook_methods at the top of your class, you will make it clear to other developers exactly which methods need to be implemented by subclasses.

Usage

gem install 'method_template'
require 'method_template'
class BaseClass
  extend MethodTemplate
  hook_methods :method_a, :method_b, :method_c     #Note that #hook_method may be used as well in case there is only one method
end

Now, if the required hook methods are not provided in a subclass, an exception is raised with a helpful message:

class Subclass < BaseClass; end
=> nil
s = Subclass.new
=> #<Subclass:0x00007fbbee1bc608>
s.method_a
NotImplementedError (:method_a is a required method of Subclass!)