Module: SleepingKingStudios::Tools::Toolbox::Mixin
- Defined in:
- lib/sleeping_king_studios/tools/toolbox/mixin.rb
Overview
Implements recursive inheritance of both class and instance methods.
Class Method Summary collapse
-
.mixin?(othermod) ⇒ true, false
Checks if the given module is itself a Mixin.
Instance Method Summary collapse
-
#included(othermod) ⇒ void
Callback invoked whenever the receiver is included in another module.
-
#prepended(othermod) ⇒ void
Callback invoked whenever the receiver is prepended into another module.
Class Method Details
.mixin?(othermod) ⇒ true, false
Checks if the given module is itself a Mixin.
54 55 56 57 58 |
# File 'lib/sleeping_king_studios/tools/toolbox/mixin.rb', line 54 def self.mixin?(othermod) return false unless othermod.is_a?(Module) othermod.singleton_class.include?(self) end |
Instance Method Details
#included(othermod) ⇒ void
This method returns an undefined value.
Callback invoked whenever the receiver is included in another module.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sleeping_king_studios/tools/toolbox/mixin.rb', line 66 def included(othermod) return super unless defined?(self::ClassMethods) if SleepingKingStudios::Tools::Toolbox::Mixin.mixin?(othermod) unless othermod.constants(false).include?(:ClassMethods) othermod.const_set(:ClassMethods, Module.new) end othermod::ClassMethods.include(self::ClassMethods) else othermod.extend self::ClassMethods end super end |
#prepended(othermod) ⇒ void
This method returns an undefined value.
Callback invoked whenever the receiver is prepended into another module.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sleeping_king_studios/tools/toolbox/mixin.rb', line 88 def prepended(othermod) return super unless defined?(self::ClassMethods) if SleepingKingStudios::Tools::Toolbox::Mixin.mixin?(othermod) unless othermod.constants(false).include?(:ClassMethods) othermod.const_set(:ClassMethods, Module.new) end othermod::ClassMethods.prepend(self::ClassMethods) else othermod.singleton_class.prepend(self::ClassMethods) end super end |