module Temple::Mixins::CompiledDispatcher

def call(exp)

def call(exp)
  compile(exp)
end

def compile(exp)

def compile(exp)
  dispatcher(exp)
end

def dispatched_methods

def dispatched_methods
  re = /^on(_[a-zA-Z0-9]+)*$/
  self.methods.map(&:to_s).select(&re.method(:=~))
end

def dispatcher(exp)

def dispatcher(exp)
  replace_dispatcher(exp)
end

def replace_dispatcher(exp)

def replace_dispatcher(exp)
  tree = DispatchNode.new
  dispatched_methods.each do |method|
    method.split('_')[1..-1].inject(tree) {|node, type| node[type.to_sym] }.method = method
  end
  self.class.class_eval %{
    def dispatcher(exp)
      if self.class == #{self.class}
        #{tree.compile}
      else
        replace_dispatcher(exp)
      end
    end
  }
  dispatcher(exp)
end