Module: AsyncRack::AsyncCallback::InheritanceHook

Defined in:
lib/async_rack/async_callback.rb

Overview

Aliases a subclass on subclassing, but only once. If that name already is in use, it will be replaced.

Examples:

class Foo
  def self.bar
    23
  end
end

Foo.extend AsyncRack::AsyncCallback::InheritanceHook
Foo.alias_subclass :Baz

class Bar < Foo
  def self.bar
    super + 19
  end
end

Baz.bar # => 42

Instance Method Summary collapse

Instance Method Details

#alias_subclass(name, namespace = Object) ⇒ Object

Parameters:

  • name (Symbol)

    Name it will be aliased to

  • namespace (Class, Module) (defaults to: Object)

    The module the constant will be defined in



61
62
63
# File 'lib/async_rack/async_callback.rb', line 61

def alias_subclass(name, namespace = Object)
  @alias_subclass = [name, namespace]
end

#inherited(klass) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/async_rack/async_callback.rb', line 67

def inherited(klass)
  super
  if @alias_subclass
    name, namespace = @alias_subclass
    @alias_subclass = nil
    namespace.send :remove_const, name if namespace.const_defined? name
    namespace.const_set name, klass
    klass.send :include, AsyncRack::AsyncCallback::Mixin
  end
end