0.0
No commit activity in last 3 years
No release in over 3 years
Adds `before_extended`, `before_included`, and `before_prepended` methods hooks which would be called before the standard `extended`, `included`, and `prepended` Ruby hooks, respectively. Especially useful when you require to "do" something just before the module gets `extended` or `included` to a module/class. In particular, in my specific case, I needed to "do" something if a specific method already exists in the `base` class.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 9.0
~> 10.0
~> 3.7.0
 Project Readme

BeforeHooks

Build Status

Adds before_extended, before_included, and before_prepended methods hooks which would be called before the standard extended, included, and prepended Ruby hooks, respectively.

Especially useful when you require to "do" something just before the module gets extended, included, or prepended to a module/class. In particular, in my specific case, I needed to "do" something first if a specific method already exists in the base class before being extended, of which then I'd use before_extended.

Dependencies

  • Ruby ~> 2.0

Installation

Add this line to your application's Gemfile:

gem 'before_hooks', '~> 0.1'

And then execute:

$ bundle

Or install it yourself as:

$ gem install before_hooks

Usage

before_included Example

require 'bundler/setup'
require 'before_hooks'

module SomeModule
  # not required to be defined
  def self.before_included(base)
    pp 'SomeModule#before_included'
    pp base
    pp base.ancestors
  end

  # not required to be defined:
  def self.included(base)
    pp 'SomeModule#included'
    pp base
    pp base.ancestors
  end
end

class SomeClass
  include SomeModule
end

# upon code execution, will print...

=begin

"SomeModule#before_included"
SomeClass
[SomeClass, Object, PP::ObjectMixin, Kernel, BasicObject]
"SomeModule#included"
SomeClass
[SomeClass, SomeModule, Object, PP::ObjectMixin, Kernel, BasicObject]

=end

before_extended Example

require 'bundler/setup'
require 'before_hooks'

module SomeModule
  # not required to be defined
  def self.before_extended(base)
    pp 'SomeModule#before_extended'
    pp base
    pp base.singleton_class.ancestors
  end

  # not required to be defined:
  def self.extended(base)
    pp 'SomeModule#extended'
    pp base
    pp base.singleton_class.ancestors
  end
end

class SomeClass
  extend SomeModule
end

# upon code execution, will print...

=begin

"SomeModule#before_extended"
SomeClass
[#<Class:SomeClass>,
 #<Class:Object>,
 #<Class:BasicObject>,
 Class,
 BeforeHooks,
 Module,
 Object,
 PP::ObjectMixin,
 Kernel,
 BasicObject]
"SomeModule#extended"
SomeClass
[#<Class:SomeClass>,
 SomeModule,
 #<Class:Object>,
 #<Class:BasicObject>,
 Class,
 BeforeHooks,
 Module,
 Object,
 PP::ObjectMixin,
 Kernel,
 BasicObject]

=end

before_prepended Example

require 'bundler/setup'
require 'before_hooks'

module SomeModule
  # not required to be defined
  def self.before_prepended(base)
    pp 'SomeModule#before_prepended'
    pp base
    pp base.ancestors
  end

  # not required to be defined:
  def self.prepended(base)
    pp 'SomeModule#prepended'
    pp base
    pp base.ancestors
  end
end

class SomeClass
  prepend SomeModule
end

# upon code execution, will print...

=begin

"SomeModule#before_prepended"
SomeClass
[SomeClass, Object, PP::ObjectMixin, Kernel, BasicObject]
"SomeModule#prepended"
SomeClass
[SomeModule, SomeClass, Object, PP::ObjectMixin, Kernel, BasicObject]

=end

TODOs

  • Need help or further research on how to support and implement before_inherited, before_method_added, and before_method_removed, because "prepend" trick doesn't readily work with them.
  • Thanks to @Valaramech for suggesting to support "block" DSL, and is now a TODO.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jrpolidario/before_hooks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Special Thanks

  • @jb3689 for suggesting to use "dynamic-matching" .respond_to? :some_method instead of .singleton_class.instance_methods.include? :some_method

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the BeforeHooks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Changelog

  • 0.1.4
    • Now using "dynamic-matching" .respond_to? :some_method instead of .singleton_class.instance_methods.include? :some_method; thanks to @jb3689
  • 0.1.3
    • Initial release