Module: Pancake::Hooks::InheritableInnerClasses

Included in:
Object
Defined in:
lib/pancake/hooks/inheritable_inner_classes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/pancake/hooks/inheritable_inner_classes.rb', line 4

def self.extended(base)
  base.class_eval do
    extlib_inheritable_reader :_inhertiable_inner_classes, :_before_inner_class_inheritance
    @_inhertiable_inner_classes      = []
    @_before_inner_class_inheritance = []
  end
end

Instance Method Details

#before_inner_class_inheritance(&blk) ⇒ Object

Runs any hooks before the inheritance of any inner classes

Author:

  • Daniel Neighman

Since:

  • 0.3.0



49
50
51
52
# File 'lib/pancake/hooks/inheritable_inner_classes.rb', line 49

def before_inner_class_inheritance(&blk)
  _before_inner_class_inheritance << blk if blk
  _before_inner_class_inheritance
end

#inheritable_inner_classes(*classes) ⇒ Object

Declare inner classes to be inherited when the outer class in inherited The best way to show this is by example:

This provides a more organic inheritance where the child gets their own version of the inner class which is actually inherited from the parents inner class. The inheritance chain remains intact.

Examples:

class Foo
  inheritable_inner_class :Bar

  class Bar
  end
end

class Baz < Foo
  # When Foo is inherited, the following occurs
  class Bar < Foo::Bar; end
end

Author:

  • Daniel Neighman

Since:

  • 0.1.0



35
36
37
38
39
40
41
42
# File 'lib/pancake/hooks/inheritable_inner_classes.rb', line 35

def inheritable_inner_classes(*classes)
  _inhertiable_inner_classes
  unless classes.empty?
    _inhertiable_inner_classes << classes
    _inhertiable_inner_classes.flatten!
  end
  _inhertiable_inner_classes
end

#inherited(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The inherited hook that sets up inherited inner classes. Remember if you overwrite this method, you should call super!

Author:

  • Daniel Neighman

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
# File 'lib/pancake/hooks/inheritable_inner_classes.rb', line 60

def inherited(base)
  super
  class_defs = inheritable_inner_classes.map do |klass|
    _before_inner_class_inheritance.each{|blk| blk.call(base.superclass) }
    "class #{klass} < superclass::#{klass}; end\n"
  end
  base.class_eval(class_defs.join)
end