lib/flows/util/prepend_to_class.rb in flows-0.5.1 vs lib/flows/util/prepend_to_class.rb in flows-0.6.0

- old
+ new

@@ -136,11 +136,12 @@ # * when `Mod` is included into some module `Mod2` - `Mod2` also will # prepend "to prepend" module when included into class. # * you can include `Mod` into `Mod2`, then include `Mod2` into `Mod3` - # desribed behavior works for include chain of any length. # - # Moreover, this behaviour also works with `extend`, not only `include`. + # Each `include` generates a new prepend. Be careful about this when including + # generated module several times in the inheritance chain. # # @yield body for module which will be prepended # @return [Module] module to be included or extended into your module def make_module(&module_body) Module.new.tap do |mod| @@ -158,12 +159,23 @@ mod.const_set(:Injector, injector) mod.singleton_class.prepend(injector) end - def make_injector_mod(module_to_prepend) + # :reek:TooManyStatements :reek:DuplicateMethodCall + def make_injector_mod(module_to_prepend) # rubocop:disable Metrics/MethodLength Module.new.tap do |injector| injector.define_method(:included) do |target_mod| + if target_mod.class == Class + target_mod.prepend(module_to_prepend) + else # Module + target_mod.singleton_class.prepend injector + end + + super(target_mod) + end + + injector.define_method(:extended) do |target_mod| if target_mod.class == Class target_mod.prepend(module_to_prepend) else # Module target_mod.singleton_class.prepend injector end