Skip to content

dblock/module-mixins

Repository files navigation

Module::Mixins

Gem Version Build Status Dependency Status Code Climate

Help with inspecting mixed-in Ruby modules.

Install

Add module-mixins to your Gemfile.

gem 'module-mixins'

Use

Find All Classes that Include a Module

In order to find all classes that include a module you would typically track those via the Module.included callback.

module MyModule
  attr_accessor :classes_including

  def included(base)
    self.classes_including ||= []
    self.classes_including << base
  end
end

This works well and is the behavior implemented in module-mixins by Module::Mixins::Included. You can extend your modules instead of duplicating code.

module MyModule
  extend Module::Mixins::Included
end

Ruby also provides a mechanism to enumerate all constants, which enables us to find mixed-in modules dynamically.

You can extend a module and call classes_including similarly.

module MyModule
  extend Module::Mixins::Including
end

You can also extend existing modules, which is quite powerful because you can apply it to modules you have not declared in your own code.

irb> Enumerable.extend Module::Mixins::Including
 => Enumerable

irb> Enumerable.classes_including
 => [Array, Hash, Struct, ..., Set, SortedSet]

You can also use a module method without needing to mix-in another module.

irb> Module::Mixins::Including.classes_including(Comparable)
 => [String, Symbol, Numeric, ..., Date, DateTime]

References

Contribute

See CONTRIBUTING.

Copyright and License

Copyright Daniel Doubrovkine and Contributors, Artsy Inc., 2015

MIT License

About

Help with inspecting mixed-in Ruby modules.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages