Class: UrlRegexp::PathSet

Inherits:
Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/url_regexp/path_set.rb

Instance Method Summary collapse

Methods inherited from Node

#to_regexp

Constructor Details

#initialize(set = nil, options = {}) ⇒ PathSet

Returns a new instance of PathSet.



18
19
20
21
22
# File 'lib/url_regexp/path_set.rb', line 18

def initialize(set = nil, options = {})
  @set = set || Set.new
  @options = options
  @wildcard_threshold = options[:wildcard_threshold] || 5
end

Instance Method Details

#&(other) ⇒ Object



39
40
41
# File 'lib/url_regexp/path_set.rb', line 39

def &(other)
  self.class.new(@set & other.set, @options)
end

#==(other) ⇒ Object Also known as: eql?



28
29
30
31
# File 'lib/url_regexp/path_set.rb', line 28

def ==(other)
  self.class == other.class &&
    @set.to_a == other.set.to_a
end

#append(path) ⇒ Object



24
25
26
# File 'lib/url_regexp/path_set.rb', line 24

def append(path)
  set << path
end

#hashObject



35
36
37
# File 'lib/url_regexp/path_set.rb', line 35

def hash
  @set.hash
end

#include?(path) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/url_regexp/path_set.rb', line 47

def include?(path)
  @set.include?(path)
end

#to_regexp_sObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/url_regexp/path_set.rb', line 51

def to_regexp_s
  if @wildcard_threshold < size
    '([^#?]*)'
  elsif 1 < size
    children_paths = map(&:paths).reduce { |a, e| a & e }
    if children_paths.size == 1 && all? { |p| !p.path_end }
      "(#{map(&:label).join('|')})/#{children_paths.to_regexp_s}"
    else
      regexps = map(&:to_regexp_s)
      match = ''
      if regexps.size > 1
        base = regexps[0]
        base.split(//).each do |c|
          tmp = match + c
          break unless regexps[1..-1].all? { |r| r.start_with? tmp }
          match = tmp
        end
        regexps = regexps.map { |r| r[match.size..-1] }
      end
      "#{match}(#{regexps.join('|')})"
    end
  elsif 1 == size
    to_a.first.to_regexp_s
  end
end

#|(other) ⇒ Object



43
44
45
# File 'lib/url_regexp/path_set.rb', line 43

def |(other)
  self.class.new(@set & other.set, @options)
end