[Nitro] Component System instead of ClassMethods hack?

TRANS transfire at gmail.com
Mon Jun 12 09:01:54 EDT 2006


Given what Matz is saying on the thread concerning ClassMethods, I
would suggest deprecating the use of class_inherit and ClassMethods,
and instead go through the Nitro/Og code and split the modules into
two, extend vs. include parts. Then use each separately.

But I realize that some of those modules have to go together, so it
seems silly that they'd be split this way, So I was thinking of a
system like this:

  class Component

    def initialize( &template )
      @extensions = []
      @inclusions = []
      instance_eval &template
    end

    def self.[]( e, i )
       c = new
       c << e
       c < i
    end

    def include( *inclusions )
      @inclusions.concat *inclusions
    end

    def extend( *extensions )
      @extensions.concat extensions
    end

    def <( inclusion )
      @inclusions << inclusion
      self
    end

    def <<( extension )
      @extensions << extension
      self
    end

    def apply_features( base )
      base.module_eval {
        extend *@extensions
        include *@inclusions
      end
    end

  end

  class Module

    def inherit( *components )
      components.each { |c|
        c.apply_features( self )
      end
    end

  end

Then we can do things like:

  module Mc
    def c ; "c" ; end
  end

  module Mi
    def i; "i"; end
  end

  # quick notation for a pair

  M = Component[ Mc, Mi ]

  # or long notaion for more complex components

  M = Component.new do
    extend Mc
    include Mi
  end

  class X
    inherit M
  end

  X.c #=> c
  X.new.i #=> i

Thoughts?

T.



More information about the Nitro-general mailing list