Class: ActionDispatch::Journey::Route

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

Defined Under Namespace

Modules: VerbMatchers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, app: nil, path:, constraints: {}, required_defaults: [], defaults: {}, request_method_match: nil, precedence: 0, scope_options: {}, internal: false) ⇒ Route

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


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 56

def initialize(name:, app: nil, path:, constraints: {}, required_defaults: [], defaults: {}, request_method_match: nil, precedence: 0, scope_options: {}, internal: false)
  @name        = name
  @app         = app
  @path        = path

  @request_method_match = request_method_match
  @constraints = constraints
  @defaults    = defaults
  @required_defaults = nil
  @_required_defaults = required_defaults
  @required_parts    = nil
  @parts             = nil
  @decorated_ast     = nil
  @precedence        = precedence
  @path_formatter    = @path.build_formatter
  @scope_options     = scope_options
  @internal          = internal
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


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

def app
  @app
end

#constraintsObject (readonly) Also known as: conditions

Returns the value of attribute constraints


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

def constraints
  @constraints
end

#defaultsObject (readonly)

Returns the value of attribute defaults


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

def defaults
  @defaults
end

#internalObject (readonly)

Returns the value of attribute internal


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

def internal
  @internal
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


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

def path
  @path
end

#precedenceObject (readonly)

Returns the value of attribute precedence


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

def precedence
  @precedence
end

#scope_optionsObject (readonly)

Returns the value of attribute scope_options


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

def scope_options
  @scope_options
end

Class Method Details

.verb_matcher(verb) ⇒ Object


47
48
49
50
51
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 47

def self.verb_matcher(verb)
  VerbMatchers::VERB_TO_CLASS.fetch(verb) do
    VerbMatchers::Unknown.new verb.to_s.dasherize.upcase
  end
end

Instance Method Details

#astObject


83
84
85
86
87
88
89
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 83

def ast
  @decorated_ast ||= begin
    decorated_ast = path.ast
    decorated_ast.find_all(&:terminal?).each { |n| n.memo = self }
    decorated_ast
  end
end

#dispatcher?Boolean

Returns:

  • (Boolean)

148
149
150
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 148

def dispatcher?
  @app.dispatcher?
end

#eager_load!Object


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

def eager_load!
  path.eager_load!
  ast
  parts
  required_defaults
  nil
end

#format(path_options) ⇒ Object


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

def format(path_options)
  @path_formatter.evaluate path_options
end

#glob?Boolean

Returns:

  • (Boolean)

144
145
146
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 144

def glob?
  path.spec.any?(Nodes::Star)
end

#ipObject


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

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

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 152

def matches?(request)
  match_verb(request) &&
  constraints.all? { |method, value|
    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

#partsObject Also known as: segment_keys


121
122
123
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 121

def parts
  @parts ||= segments.map(&:to_sym)
end

#required_default?(key) ⇒ Boolean

Returns:

  • (Boolean)

134
135
136
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 134

def required_default?(key)
  @_required_defaults.include?(key)
end

#required_defaultsObject


138
139
140
141
142
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 138

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

#required_keysObject


109
110
111
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 109

def required_keys
  required_parts + required_defaults.keys
end

#required_partsObject


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

def required_parts
  @required_parts ||= path.required_names.map(&:to_sym)
end

#requirementsObject

Needed for `bin/rails routes`. Picks up succinctly defined requirements for a route, for example route

get 'photo/:id', :controller => 'photos', :action => 'show',
  :id => /[A-Z]\d{5}/

will have :action=>“show”, :id=>/d{5/} as requirements.


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

def requirements
  @defaults.merge(path.requirements).delete_if { |_, v|
    /.+?/ == v
  }
end

#requires_matching_verb?Boolean

Returns:

  • (Boolean)

174
175
176
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 174

def requires_matching_verb?
  !@request_method_match.all? { |x| x == VerbMatchers::All }
end

#score(supplied_keys) ⇒ Object


113
114
115
116
117
118
119
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 113

def score(supplied_keys)
  path.required_names.each do |k|
    return -1 unless supplied_keys.include?(k)
  end

  (required_defaults.length * 2) + path.names.count { |k| supplied_keys.include?(k) }
end

#segmentsObject


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

def segments
  path.names
end

#verbObject


178
179
180
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 178

def verb
  verbs.join("|")
end