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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pattern.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 9

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



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

def anchored
  @anchored
end

#astObject (readonly)

Returns the value of attribute ast



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

def ast
  @ast
end

#namesObject (readonly)

Returns the value of attribute names



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

def names
  @names
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

Instance Method Details

#build_formatterObject



23
24
25
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 23

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

#eager_load!Object



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

def eager_load!
  required_names
  offsets
  to_regexp
  @ast = nil
end

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



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

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

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#optional_namesObject



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

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

#required_namesObject



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

def required_names
  @required_names ||= names - optional_names
end

#requirements_anchored?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 34

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



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

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

#sourceObject



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

def source
  to_regexp.source
end

#to_regexpObject



170
171
172
# File 'actionpack/lib/action_dispatch/journey/path/pattern.rb', line 170

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