apeiros / inherit

Avoid the anti-pattern of `def self.included(base); base.extend …; end`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

README

Summary

Avoid the anti-pattern of def self.included(base); base.extend …; end, without getting lost in subclassing.

Installation

gem install inherit

Usage

require 'inherit'

module Inheritable
  module Constants
    Foo = "Nice constant!"
  end
  module ClassMethods
    def funky
      "Funky class method!"
    end
  end
  module InstanceMethods
    def rad
      "Rad instance method!"
    end
  end
  def self.inherited(subclass)
    puts "#{self} got inherited by #{subclass}"
  end
  def self.extended(object)
    puts "#{self} extended #{object}"
  end
end

class Example
  inherit Inheritable # -> "Inheritable got inherited by Example"
end
Example::Foo                  # => "Nice constant!"
Example.funky                 # => "Funky class method!"
Example.new.rad               # => "Rad instance method!"
a_string = "a string"
a_string.inherit Inheritable  # -> "Inheritable extended a string"
a_string.singleton_class::Foo # => "Nice constant!"
a_string.rad                  # => "Rad instance method!"

Description

Avoid the anti-pattern of def self.included(base); base.extend …; end, without getting lost in subclassing.

Links

License

You can use this code under the {file:LICENSE.txt BSD-2-Clause License}, free of charge. If you need a different license, please ask the author.

About

Avoid the anti-pattern of `def self.included(base); base.extend …; end`

License:BSD 2-Clause "Simplified" License


Languages

Language:CSS 52.5%Language:JavaScript 27.5%Language:Ruby 20.0%