Project

added

0.0
No release in over 3 years
Low commit activity in last 3 years
Module#added: A unified module hook to run code on all instances when adding the module.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Added Hook for Ruby [version] [ci]

Module#added: A unified module hook to run code on all instances when adding the module.

Warning

This is experimental: You should exactly know what you do, if you want to use it in production.

Besides this, I am really curious if you like the approach this gem is taking!

Description

Ruby allows you to run hooks, when an module is inserted into another object:

  • Module#extended
  • Module#included
  • Module#prepended

This gem unifies all these hooks into a single Module#added one, which will be fired for all instances that the module has been added to. An example use case might be that you want to set some instance variable for all instances that include the module. There are three different occasions a hook is fired:

  • An object extends itself with the module -> The added hook will run for this object
  • A class has included/prepended the module -> The added hook will run on newly initialized objects of this class
  • A class has included/prepended the module -> The added hook will run on all existing instances of this class

Setup

Add to your Gemfile

gem 'added'

Usage

module MyStateSetter
  def self.added(instance)
    instance.instance_variable_set(:@my, "state")
  end
end

# Extending
object = Object.new
object.extend(MyStateSetter)
object.instance_variable_get(:@my) # => "state"

# Including (new instances)
klass = Class.new
klass.include(MyStateSetter)
object = klass.new
object.instance_variable_get(:@my) # => "state"

# Including (existing instances)
klass = Class.new
object = klass.new
klass.include(MyStateSetter)
object.instance_variable_get(:@my) # => "state"

# Prepending (new instances)
klass = Class.new
klass.prepend(MyStateSetter)
object = klass.new
object.instance_variable_get(:@my) # => "state"

# Including (existing instances)
klass = Class.new
object = klass.new
klass.prepend(MyStateSetter)
object.instance_variable_get(:@my) # => "state"

JRuby Notes

This gem requires a Module#prepend implementation and must have the ObjectSpace available. As JRuby user this means you will need to run JRuby with the -X+O option to use this gem.

Also See

MIT License

Copyright (C) 2015 by Jan Lelis.