Sha256: f79c368b7d128dc07b0f0d6cc1858c84b6aef14feaada12c7974b0d39d872d03

Contents?: true

Size: 796 Bytes

Versions: 1

Compression:

Stored size: 796 Bytes

Contents

module UrlRegexp
  class Query
    def initialize(options = {})
      @include_nil = false
      @queries = []
      @options = options
    end

    def append(query)
      query = query.to_s
      if query.empty?
        @include_nil = true
        return
      end
      @queries << query.split('&')
    end

    def to_regexp_s
      common_queries = @queries.reduce { |a, e| a & e } || []
      common_queries = common_queries.map { |q| Regexp.quote(q) }
      regexp_s = if 1 < common_queries.size
        "\\?(#{common_queries.permutation.map { |qs| "(.*&)?#{qs.join('.*&')}(&.*)?" }.join('|')})"
      elsif 1 == common_queries.size
        "\\?(.*&)?#{common_queries.first}(&.*)?"
      else
        '(\\?.*)?'
      end
      @include_nil ? "(#{regexp_s})?" : regexp_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
url_regexp-0.1.3 lib/url_regexp/query.rb