Class: UrlRegexp::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/url_regexp/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Query

Returns a new instance of Query.



3
4
5
6
7
# File 'lib/url_regexp/query.rb', line 3

def initialize(options = {})
  @include_nil = false
  @queries = []
  @options = options
end

Instance Method Details

#append(query) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/url_regexp/query.rb', line 9

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

#to_regexp_sObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/url_regexp/query.rb', line 18

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