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

Inherits:
Object
  • Object
show all
Defined in:
actionpack/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.


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

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


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

def anchored
  @anchored
end

#requirementsObject (readonly)

Returns the value of attribute requirements


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

def requirements
  @requirements
end

#specObject (readonly)

Returns the value of attribute spec


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

def spec
  @spec
end

Class Method Details

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


13
14
15
16
17
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 13

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


9
10
11
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 9

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

Instance Method Details

#astObject


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

def ast
  @spec.each do |node|
    if node.symbol?
      re = @requirements[node.to_sym]
      node.regexp = re if re
    elsif node.star?
      node = node.left
      node.regexp = @requirements[node.to_sym] || /(.+)/
    end
  end

  @spec
end

#build_formatterObject


32
33
34
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 32

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

#eager_load!Object


36
37
38
39
40
41
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 36

def eager_load!
  required_names
  offsets
  to_regexp
  nil
end

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


162
163
164
165
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 162

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

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)

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

def match?(other)
  to_regexp.match?(other)
end

#namesObject


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

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

#optional_namesObject


65
66
67
68
69
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 65

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

#required_namesObject


61
62
63
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 61

def required_names
  @required_names ||= names - optional_names
end

#requirements_for_missing_keys_checkObject


180
181
182
183
184
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 180

def requirements_for_missing_keys_check
  @requirements_for_missing_keys_check ||= requirements.transform_values do |regex|
    /\A#{regex}\Z/
  end
end

#sourceObject


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

def source
  to_regexp.source
end

#to_regexpObject


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

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