/include_with_respect

Find out if your include/extend hooks are misbehaving!

Primary LanguageRubyMIT LicenseMIT

IncludeWithRespect

Find out if your include/extend hooks are misbehaving!

Why did I make this gem?

Modules have hooks on include and extend, among others. These will run every time a module is included or extended into another module or class. If the hooks should only run once, (think shared state), then running them multiple times can cause difficult to trace bugs. This gem allows developers to trace modules that are re-included multiple times into other objects.

Project IncludeWithRespect
gem name include_with_respect
compatibility Ruby 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7
license License: MIT
download rank Downloads Today
version Version
dependencies Depfu
continuous integration Build Status
test coverage Test Coverage
maintainability Maintainability
code triage Open Source Helpers
homepage on Github.com, on Railsbling.com
documentation on RubyDoc.info
Spread ♡ⓛⓞⓥⓔ♡ 🌏, 👼, :shipit:, Tweet Peter

Installation

Add this line to your application's Gemfile:

gem 'include_with_respect'

And then execute:

$ bundle

Or install it yourself as:

$ gem install include_with_respect

Usage

With Ruby Class:

module SomeOtherModule
  def self.included(base)
    base.send(:include, SomeModule)
  end
end

class MyClass
  include IncludeWithRespect::ModuleWithRespect
  include SomeModule
  include SomeOtherModule
end

Because SomeOtherModule also includes SomeModule two things will happen that normally do not:

  1. a warning will be printed (via puts)
  2. the duplicate inclusion will be skipped (meaning the hooks will not run again)
  • This is a major change in the behavior of including modules, but normally hooks running multiple times is not desired, or intended, which is why this gem exists!

With Ruby Module:

module MyModule
  def self.included(base)
    base.send(:include, IncludeWithRespect::ModuleWithRespect)
    base.send(:include, SomeModule)
  end
end

Then if MyModule is included somewhere that also includes SomeModule two things will happen that normally do not:

  1. a warning will be printed (via puts)
  2. the duplicate inclusion will be skipped (meaning the hooks will not run again)
  • This is a major change in the behavior of including modules, but normally hooks running multiple times is not desired, or intended, which is why this gem exists!

With Rails' ActiveSupport::Concern

module MyConcern
  extend ActiveSupport::Concern
  included do
    include IncludeWithRespect::ConcernWithRespect
    include SomeOtherConcern
  end
end

Then if MyConcern is included somewhere that also includes SomeOtherConcern two things will happen that normally do not:

  1. a warning will be printed (via puts)
  2. the duplicate inclusion will be skipped (meaning the hooks will not run again)
  • This is a major change in the behavior of including modules, but normally hooks running multiple times is not desired, or intended, which is why this gem exists!

Raw Usage (more intrusive code changes)

module SomeOtherModule
  def self.included(base)
    base.send(:include, SomeModule)
  end
end

class MyClass
  include IncludeWithRespect
  include_with_respect SomeModule
  include_with_respect SomeOtherModule
end

Same results as the other usage examples.

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/pboling/include_with_respect.

Create an issue and tell me about it, or fix it yo'sef.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  6. Create new Pull Request

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency 'include_with_respect', '~> 0.0'

Legal