Sha256: 887be2d52a8008e3c509c2a14f2f7ee96eb2a2fd954d3d7559867154eba1f24e

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

require 'mustermann'

module Angelo

  module Mustermann

    # hrm, sneaky
    #
    def self.included base
      base.extend ClassMethods
      base.class_eval do
        def_delegator :@responder, :mustermann
      end

      [Responder, WebsocketResponder].each do |res|
        res.class_eval do
          attr_accessor :mustermann
        end
      end
    end

    module ClassMethods

      HTTPABLE.each do |m|
        define_method m do |path, &block|
          path = ::Mustermann.new path
          routes[m][path] = Responder.new &block
        end
      end

      def socket path, &block
        path = ::Mustermann.new path
        routes[:socket][path] = WebsocketResponder.new &block
      end

      def routes
        @routes ||= {}
        ROUTABLE.each do |m|
          @routes[m] ||= RouteMap.new
        end
        @routes
      end

    end

    def params
      @params ||= super.merge mustermann.params(request.path)
    end

    class RouteMap < Hash
      def [] route
        responder = nil
        mustermann = keys.select {|k| k.match(route)}.first
        if mustermann
          responder = fetch mustermann
          responder.mustermann = mustermann
        end
        responder
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
angelo-0.1.7 lib/angelo/mustermann.rb
angelo-0.1.6 lib/angelo/mustermann.rb
angelo-0.1.5 lib/angelo/mustermann.rb
angelo-0.1.4 lib/angelo/mustermann.rb
angelo-0.1.3 lib/angelo/mustermann.rb
angelo-0.1.2 lib/angelo/mustermann.rb
angelo-0.1.1 lib/angelo/mustermann.rb
angelo-0.1.0 lib/angelo/mustermann.rb