Help with inspecting mixed-in Ruby modules.
Add module-mixins
to your Gemfile.
gem 'module-mixins'
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]
- https://www.ruby-forum.com/topic/163430
- http://stackoverflow.com/questions/3917857/getting-a-list-of-classes-that-include-a-module
See CONTRIBUTING.
Copyright Daniel Doubrovkine and Contributors, Artsy Inc., 2015