Sha256: 3d51e592fcf127f8850d2b62045b74d812b546dba6046dfd686db34120803067

Contents?: true

Size: 1.81 KB

Versions: 18

Compression:

Stored size: 1.81 KB

Contents

module Pancake
  module Hooks 
    module InheritableInnerClasses
      def self.extended(base)
        base.class_eval do
          class_inheritable_reader :_inhertiable_inner_classes
          @_inhertiable_inner_classes = []
        end
      end # extended
  
      # Declare inner classes to be inherited when the outer class in inherited
      # The best way to show this is by example:
      #
      # @example 
      #   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
      #
      # 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.
      #
      # @api public
      # @since 0.1.0
      # @author Daniel Neighman
      def inheritable_inner_classes(*classes)
        _inhertiable_inner_classes
        unless classes.empty?
          _inhertiable_inner_classes << classes
          _inhertiable_inner_classes.flatten!
        end
        _inhertiable_inner_classes
      end
      
      # The inherited hook that sets up inherited inner classes.  Remember if you overwrite this method, you should
      # call super!
      #
      # @api private
      # @since 0.1.0
      # @author Daniel Neighman
      def inherited(base)
        super
        class_defs = inheritable_inner_classes.map do |klass|
          "class #{klass} < superclass::#{klass}; end\n"
        end
        # puts "#{base.name} is INHERITING INNER CLASSES #{class_defs.inspect}"
        base.class_eval(class_defs.join)
      end
      
    end # InheritableInnerClasses
  end # Hooks
end # Pancake

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
hassox-pancake-0.1.6 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.29 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.28 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.27 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.26 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.25 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.24 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.22 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.20 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.19 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.18 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.17 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.16 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.15 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.13 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.12 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.10 lib/pancake/hooks/inheritable_inner_classes.rb
pancake-0.1.8 lib/pancake/hooks/inheritable_inner_classes.rb