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

Constant Summary collapse

REGEXP_CACHE =
{}

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
31
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 19

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

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

Instance Attribute Details

#anchoredObject (readonly)

Returns the value of attribute anchored.



17
18
19
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 17

def anchored
  @anchored
end

#astObject (readonly)

Returns the value of attribute ast.



17
18
19
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 17

def ast
  @ast
end

#namesObject (readonly)

Returns the value of attribute names.



17
18
19
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 17

def names
  @names
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



17
18
19
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 17

def requirements
  @requirements
end

#specObject (readonly)

Returns the value of attribute spec.



17
18
19
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 17

def spec
  @spec
end

Class Method Details

.dedup_regexp(regexp) ⇒ Object



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

def dedup_regexp(regexp)
  REGEXP_CACHE[regexp.source] ||= regexp
end

Instance Method Details

#build_formatterObject



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

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

#eager_load!Object



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

def eager_load!
  required_names
  offsets
  to_regexp
  @ast = nil
end

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



167
168
169
170
# File 'actionpack/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

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#optional_namesObject



70
71
72
73
74
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 70

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

#required_namesObject



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

def required_names
  @required_names ||= names - optional_names
end

#requirements_anchored?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 44

def requirements_anchored?
  # each required param must not be surrounded by a literal, otherwise it isn't
  # simple to chunk-match the url piecemeal
  terminals = ast.terminals

  terminals.each_with_index { |s, index|
    next if index < 1
    next if s.type == :DOT || s.type == :SLASH

    back = terminals[index - 1]
    fwd = terminals[index + 1]

    # we also don't support this yet, constraints must be regexps
    return false if s.symbol? && s.regexp.is_a?(Array)

    return false if back.literal?
    return false if !fwd.nil? && fwd.literal?
  }

  true
end

#requirements_for_missing_keys_checkObject



185
186
187
188
189
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 185

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

#sourceObject



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

def source
  to_regexp.source
end

#to_regexpObject



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

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