Class: ActionDispatch::Journey::Path::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/journey/path/pattern.rb

Overview

:nodoc:

Defined Under Namespace

Classes: AnchoredRegexp, MatchData, RegexpOffsets, UnanchoredRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strexp) ⇒ Pattern

Returns a new instance of Pattern.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/action_dispatch/journey/path/pattern.rb', line 7

def initialize(strexp)
  parser = Journey::Parser.new

  @anchored = true

  case strexp
  when String
    @spec         = parser.parse(strexp)
    @requirements = {}
    @separators   = "/.?"
  when Router::Strexp
    @spec         = parser.parse(strexp.path)
    @requirements = strexp.requirements
    @separators   = strexp.separators.join
    @anchored     = strexp.anchor
  else
    raise ArgumentError, "Bad expression: #{strexp}"
  end

  @names          = nil
  @optional_names = nil
  @required_names = nil
  @re             = nil
  @offsets        = nil
end

Instance Attribute Details

#anchoredObject (readonly)

Returns the value of attribute anchored.



5
6
7
# File 'lib/action_dispatch/journey/path/pattern.rb', line 5

def anchored
  @anchored
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



5
6
7
# File 'lib/action_dispatch/journey/path/pattern.rb', line 5

def requirements
  @requirements
end

#specObject (readonly)

Returns the value of attribute spec.



5
6
7
# File 'lib/action_dispatch/journey/path/pattern.rb', line 5

def spec
  @spec
end

Instance Method Details

#astObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/action_dispatch/journey/path/pattern.rb', line 33

def ast
  @spec.grep(Nodes::Symbol).each do |node|
    re = @requirements[node.to_sym]
    node.regexp = re if re
  end

  @spec.grep(Nodes::Star).each do |node|
    node = node.left
    node.regexp = @requirements[node.to_sym] || /(.+)/
  end

  @spec
end

#match(other) ⇒ Object Also known as: =~



167
168
169
170
# File 'lib/action_dispatch/journey/path/pattern.rb', line 167

def match(other)
  return unless match = to_regexp.match(other)
  MatchData.new(names, offsets, match)
end

#namesObject



47
48
49
# File 'lib/action_dispatch/journey/path/pattern.rb', line 47

def names
  @names ||= spec.grep(Nodes::Symbol).map { |n| n.name }
end

#optional_namesObject



55
56
57
58
59
# File 'lib/action_dispatch/journey/path/pattern.rb', line 55

def optional_names
  @optional_names ||= spec.grep(Nodes::Group).map { |group|
    group.grep(Nodes::Symbol)
  }.flatten.map { |n| n.name }.uniq
end

#required_namesObject



51
52
53
# File 'lib/action_dispatch/journey/path/pattern.rb', line 51

def required_names
  @required_names ||= names - optional_names
end

#sourceObject



173
174
175
# File 'lib/action_dispatch/journey/path/pattern.rb', line 173

def source
  to_regexp.source
end

#to_regexpObject



177
178
179
# File 'lib/action_dispatch/journey/path/pattern.rb', line 177

def to_regexp
  @re ||= regexp_visitor.new(@separators, @requirements).accept spec
end