Skip to content

eventide-project/template-method

Repository files navigation

Template Method

Template method declaration.

Summary

A template method is a method, which is inheritable and overridable.

When developing framework style code, it is often required to override or implement a specific method in order to satisfy a contract. The purpose of this library is to provide a way for a developer of framework code to convey that intent.

The template-method library adds a template_method macro to a class. The macro declares a method, which may be implemented in a class or subclass.

The template method macro accepts an optional block. The optional block is a default implementation of the method, which is called unless a concrete implementation is provided.

The library adds also a variant of template method macro: template_method!. The variant also declares a template method, but which must be implemented in a class or subclass. If it is not implemented, an error will be raised when the method is called.

Activation

The template_method and template_method! macros can be added to all classes with:

require 'template_method'
TemplateMethod.activate

To avoid modifying the Object class, include the TemplateMethod module directly in a class rather than activating it globally with TemplateMethod.activate.

Example

class Something
  template_method :some_method

  template_method :some_other_method do |arg1, arg2|
    # some implementation
  end
end

If the template_method library is not activated, the module can be included in the class:

class Something
  include TemplateMethod

  template_method :some_method

  template_method :some_other_method do |arg1, arg2|
    # some implementation
  end
end

Implementation is Required

class Something
  include TemplateMethod

  template_method! :some_method
end

License

The template-method library is released under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •