Sha256: 3052e88c31190988b0fb59d4065a6044b322d8be6706e201ce17d226204d3e69

Contents?: true

Size: 879 Bytes

Versions: 6

Compression:

Stored size: 879 Bytes

Contents

# ~*~ encoding: utf-8 ~*~
module Aladdin

  module Support

    # Sinatra route matcher that matches mutltiple paths given in an array.
    class OneOfMatcher

      Match = Struct.new(:captures)

      # Creates a new matcher for +routes+.
      # @param [Array] routes           array of static paths
      def initialize(routes)
        @routes = routes.map { |r| '/' + r }
        @captures = Match.new []
      end

      # Matches +routes+ against +str+.
      def match(str)
        @captures[:captures] = [str]
        @captures if @routes.any? { |r| str.starts_with? r }
      end

    end

    # Sinatra route matcher that matches mutltiple paths given in an array.
    module OneOfPattern
      # @example
      #   get one_of(%w(x y z)) do
      #     puts 'Hello!'
      #   end
      def one_of(routes)
        OneOfMatcher.new routes
      end
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aladdin-0.2 lib/aladdin/support/multiroute.rb
aladdin-0.1.0.pre.1 lib/aladdin/support/multiroute.rb
aladdin-0.1.0.pre lib/aladdin/support/multiroute.rb
aladdin-0.0.8 lib/aladdin/support/multiroute.rb
aladdin-0.0.7 lib/aladdin/support/multiroute.rb
aladdin-0.0.6 lib/aladdin/support/multiroute.rb