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.

Examples:

Defining A Mixin

module Widgets
  extend SleepingKingStudios::Tools::Toolbox::Mixin

  module ClassMethods
    def widget_types
      %w(gadget doohickey thingamabob)
    end
  end

  def widget?(widget_type)
    self.class.widget_types.include?(widget_type)
  end
end

Including A Mixin

module WidgetBuilding
  extend  SleepingKingStudios::Tools::Toolbox::Mixin
  include Widgets

  def build_widget(widget_type)
    raise ArgumentError, 'not a widget', caller unless widget?(widget_type)

    Widget.new(widget_type)
  end
end

Using A Mixin

class WidgetFactory
  include WidgetBuilding
end

factory = WidgetFactory.new

factory.build_widget('gadget')
#=> Widget

WidgetFactory.widget_types
#=> ['gadget', 'doohickey', 'thingamabob']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mixin?(othermod) ⇒ true, false

Checks if the given module is itself a Mixin.

Parameters:

  • othermod (Object)

    the object or module to inspect.

Returns:

  • (true, false)

    true if the other object is a Module that extends Mixin; otherwise false.



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.

Parameters:

  • othermod (Module)

    the other class or module in which the mixin is included.



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.

Parameters:

  • othermod (Module)

    the other class or module in which the mixin is prepended.



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