module Roda::RodaPlugins::TypeRouting

def self.configure(app, opts = {})

def self.configure(app, opts = {})
  config = (app.opts[:type_routing] || CONFIGURATION).dup
  [:use_extension, :use_header, :default_type].each do |key|
    config[key] = opts[key] if opts.has_key?(key)
  end
  types = config[:types] = config[:types].dup
  mimes = config[:mimes] = config[:mimes].dup
  Array(opts[:exclude]).each do |type|
    types.delete(type)
    mimes.reject!{|_, v| v == type}
  end
  if mapping = opts[:types]
    types.merge!(mapping)
    mapping.each do |k, v|
      if v
        mimes[v.split(';', 2).first] = k
      end
    end
  end
  types.freeze
  mimes.freeze
  type_keys = config[:types].keys
  config[:extension_regexp] = /(.*?)\.(#{Regexp.union(type_keys.map(&:to_s))})\z/
  type_keys.each do |type|
    app::RodaRequest.send(:define_method, type) do |&block|
      on_type(type, &block)
    end
    app::RodaRequest.send(:alias_method, type, type)
  end
  app.opts[:type_routing] = config.freeze
end