Class: UrlRegexp::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#to_regexp

Constructor Details

#initialize(label = nil, parent = nil, options = {}) ⇒ Path

Returns a new instance of Path.



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

def initialize(label = nil, parent = nil, options = {})
  @label = label
  @parent = parent
  @options = options
  @paths = PathSet.new(nil, @options)
  @path_end = false
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



6
7
8
# File 'lib/url_regexp/path.rb', line 6

def label
  @label
end

#path_endObject

Returns the value of attribute path_end.



7
8
9
# File 'lib/url_regexp/path.rb', line 7

def path_end
  @path_end
end

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/url_regexp/path.rb', line 6

def paths
  @paths
end

Instance Method Details

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



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

def ==(other)
  self.class == other.class &&
    @label == other.label &&
    @paths == other.paths &&
    @path_end == other.path_end
end

#append(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/url_regexp/path.rb', line 30

def append(path)
  if path == ''
    @paths.append(Path.new('', self, @options))
  elsif @parent.nil?
    _, label, rest = path.split('/', 3)
  else
    label, rest = path.split('/', 2)
  end
  return if label.nil?

  p = @paths.find { |pp| pp.label == label }
  if p.nil?
    p = Path.new(label, self, @options)
    @paths.append(p)
  end
  if rest.nil?
    p.path_end = true
  else
    p.append(rest)
  end
end

#hashObject



26
27
28
# File 'lib/url_regexp/path.rb', line 26

def hash
  [@label, @paths, @path_end].hash
end

#to_regexp_sObject



52
53
54
55
56
57
58
# File 'lib/url_regexp/path.rb', line 52

def to_regexp_s
  s = @paths.to_regexp_s
  s = "/#{s}" unless s.nil?
  s = "(#{s})?" if @path_end && s.to_s != ''
  s = "#{Regexp.quote(@label)}#{s}" if @label
  s
end