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, UnanchoredRegexp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, requirements, separators, anchored) ⇒ Pattern

Returns a new instance of Pattern.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/action_dispatch/journey/path/pattern.rb', line 17

def initialize(ast, requirements, separators, anchored)
  @spec         = ast
  @requirements = requirements
  @separators   = separators
  @anchored     = anchored

  @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

Class Method Details

.build(path, requirements, separators, anchored) ⇒ Object



11
12
13
14
15
# File 'lib/action_dispatch/journey/path/pattern.rb', line 11

def self.build(path, requirements, separators, anchored)
  parser = Journey::Parser.new
  ast = parser.parse path
  new ast, requirements, separators, anchored
end

.from_string(string) ⇒ Object



7
8
9
# File 'lib/action_dispatch/journey/path/pattern.rb', line 7

def self.from_string(string)
  build(string, {}, "/.?", true)
end

Instance Method Details

#astObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_dispatch/journey/path/pattern.rb', line 41

def ast
  @spec.find_all(&:symbol?).each do |node|
    re = @requirements[node.to_sym]
    node.regexp = re if re
  end

  @spec.find_all(&:star?).each do |node|
    node = node.left
    node.regexp = @requirements[node.to_sym] || /(.+)/
  end

  @spec
end

#build_formatterObject



30
31
32
# File 'lib/action_dispatch/journey/path/pattern.rb', line 30

def build_formatter
  Visitors::FormatBuilder.new.accept(spec)
end

#eager_load!Object



34
35
36
37
38
39
# File 'lib/action_dispatch/journey/path/pattern.rb', line 34

def eager_load!
  required_names
  offsets
  to_regexp
  nil
end

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



155
156
157
158
# File 'lib/action_dispatch/journey/path/pattern.rb', line 155

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

#namesObject



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

def names
  @names ||= spec.find_all(&:symbol?).map(&:name)
end

#optional_namesObject



63
64
65
66
67
# File 'lib/action_dispatch/journey/path/pattern.rb', line 63

def optional_names
  @optional_names ||= spec.find_all(&:group?).flat_map { |group|
    group.find_all(&:symbol?)
  }.map(&:name).uniq
end

#required_namesObject



59
60
61
# File 'lib/action_dispatch/journey/path/pattern.rb', line 59

def required_names
  @required_names ||= names - optional_names
end

#sourceObject



161
162
163
# File 'lib/action_dispatch/journey/path/pattern.rb', line 161

def source
  to_regexp.source
end

#to_regexpObject



165
166
167
# File 'lib/action_dispatch/journey/path/pattern.rb', line 165

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