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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Pattern.



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

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.



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

def anchored
  @anchored
end

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

#namesObject (readonly)

Returns the value of attribute names.



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

def names
  @names
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



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

def requirements
  @requirements
end

#specObject (readonly)

Returns the value of attribute spec.



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

def spec
  @spec
end

Instance Method Details

#build_formatterObject



25
26
27
# File 'lib/action_dispatch/journey/path/pattern.rb', line 25

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

#eager_load!Object



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

def eager_load!
  required_names
  offsets
  to_regexp
  @ast = nil
end

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



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

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

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#optional_namesObject



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

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

#required_namesObject



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

def required_names
  @required_names ||= names - optional_names
end

#requirements_anchored?Boolean

Returns:

  • (Boolean)


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

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



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

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

#sourceObject



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

def source
  to_regexp.source
end

#to_regexpObject



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

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