Class: ActionDispatch::Journey::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/journey/route.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app, path, constraints, defaults = {}) ⇒ Route

path is a path constraint. constraints is a hash of constraints to be applied to this route.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/action_dispatch/journey/route.rb', line 14

def initialize(name, app, path, constraints, defaults = {})
  @name        = name
  @app         = app
  @path        = path

  # Unwrap any constraints so we can see what's inside for route generation.
  # This allows the formatter to skip over any mounted applications or redirects
  # that shouldn't be matched when using a url_for without a route name.
  while app.is_a?(Routing::Mapper::Constraints) do
    app = app.app
  end
  @dispatcher  = app.is_a?(Routing::RouteSet::Dispatcher)

  @constraints = constraints
  @defaults    = defaults
  @required_defaults = nil
  @required_parts    = nil
  @parts             = nil
  @decorated_ast     = nil
  @precedence        = 0
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/action_dispatch/journey/route.rb', line 4

def app
  @app
end

#constraintsObject (readonly) Also known as: conditions

Returns the value of attribute constraints.



6
7
8
# File 'lib/action_dispatch/journey/route.rb', line 6

def constraints
  @constraints
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/action_dispatch/journey/route.rb', line 4

def defaults
  @defaults
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/action_dispatch/journey/route.rb', line 4

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/action_dispatch/journey/route.rb', line 4

def path
  @path
end

#precedenceObject

Returns the value of attribute precedence.



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

def precedence
  @precedence
end

Instance Method Details

#astObject



36
37
38
39
40
41
42
# File 'lib/action_dispatch/journey/route.rb', line 36

def ast
  @decorated_ast ||= begin
    decorated_ast = path.ast
    decorated_ast.grep(Nodes::Terminal).each { |n| n.memo = self }
    decorated_ast
  end
end

#dispatcher?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/action_dispatch/journey/route.rb', line 104

def dispatcher?
  @dispatcher
end

#format(path_options) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/action_dispatch/journey/route.rb', line 74

def format(path_options)
  path_options.delete_if do |key, value|
    value.to_s == defaults[key].to_s && !required_parts.include?(key)
  end

  Visitors::Formatter.new(path_options).accept(path.spec)
end

#ipObject



127
128
129
# File 'lib/action_dispatch/journey/route.rb', line 127

def ip
  constraints[:ip] || //
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/action_dispatch/journey/route.rb', line 108

def matches?(request)
  constraints.all? do |method, value|
    next true unless request.respond_to?(method)

    case value
    when Regexp, String
      value === request.send(method).to_s
    when Array
      value.include?(request.send(method))
    when TrueClass
      request.send(method).present?
    when FalseClass
      request.send(method).blank?
    else
      value === request.send(method)
    end
  end
end

#optimized_pathObject



82
83
84
# File 'lib/action_dispatch/journey/route.rb', line 82

def optimized_path
  Visitors::OptimizedPath.new.accept(path.spec)
end

#optional_partsObject



86
87
88
# File 'lib/action_dispatch/journey/route.rb', line 86

def optional_parts
  path.optional_names.map { |n| n.to_sym }
end

#partsObject Also known as: segment_keys



69
70
71
# File 'lib/action_dispatch/journey/route.rb', line 69

def parts
  @parts ||= segments.map { |n| n.to_sym }
end

#required_default?(key) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/action_dispatch/journey/route.rb', line 94

def required_default?(key)
  (constraints[:required_defaults] || []).include?(key)
end

#required_defaultsObject



98
99
100
101
102
# File 'lib/action_dispatch/journey/route.rb', line 98

def required_defaults
  @required_defaults ||= @defaults.dup.delete_if do |k,_|
    parts.include?(k) || !required_default?(k)
  end
end

#required_keysObject



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

def required_keys
  required_parts + required_defaults.keys
end

#required_partsObject



90
91
92
# File 'lib/action_dispatch/journey/route.rb', line 90

def required_parts
  @required_parts ||= path.required_names.map { |n| n.to_sym }
end

#requirementsObject

:nodoc:



44
45
46
47
48
49
# File 'lib/action_dispatch/journey/route.rb', line 44

def requirements # :nodoc:
  # needed for rails `rake routes`
  path.requirements.merge(@defaults).delete_if { |_,v|
    /.+?/ == v
  }
end

#score(constraints) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/action_dispatch/journey/route.rb', line 59

def score(constraints)
  required_keys = path.required_names
  supplied_keys = constraints.map { |k,v| v && k.to_s }.compact

  return -1 unless (required_keys - supplied_keys).empty?

  score = (supplied_keys & path.names).length
  score + (required_defaults.length * 2)
end

#segmentsObject



51
52
53
# File 'lib/action_dispatch/journey/route.rb', line 51

def segments
  path.names
end

#verbObject



131
132
133
# File 'lib/action_dispatch/journey/route.rb', line 131

def verb
  constraints[:request_method] || //
end