Tools

A library of utility services and concerns.

Module: SleepingKingStudios::Tools::Toolbox::Mixin

Parent Namespace
SleepingKingStudios::Tools::Toolbox
Defined In
lib/sleeping_king_studios/tools/toolbox/mixin.rb

Table Of Contents

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']

Back To Top

Class Methods

.mixin?(othermod) => true, false

Checks if the given module is itself a Mixin.

Parameters

Returns

Back To Top

Instance Methods

#included(othermod) => void

Callback invoked whenever the receiver is included in another module.

Parameters

Returns

#prepended(othermod) => void

Callback invoked whenever the receiver is prepended into another module.

Parameters

Returns

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools | SleepingKingStudios::Tools::Toolbox