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, path, constraints, required_defaults, defaults, request_method_match, precedence, internal = false) ⇒ Route

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


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

def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, 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
  @internal          = internal
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


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

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


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

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


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

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


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

def path
  @path
end

#precedenceObject (readonly)

Returns the value of attribute precedence


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

def precedence
  @precedence
end

Class Method Details

.build(name, app, path, constraints, required_defaults, defaults) ⇒ Object


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

def self.build(name, app, path, constraints, required_defaults, defaults)
  request_method_match = verb_matcher(constraints.delete(:request_method))
  new name, app, path, constraints, required_defaults, defaults, request_method_match, 0
end

.verb_matcher(verb) ⇒ Object


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

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


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

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)

137
138
139
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 137

def dispatcher?
  @app.dispatcher?
end

#format(path_options) ⇒ Object


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

def format(path_options)
  @path_formatter.evaluate path_options
end

#glob?Boolean

Returns:

  • (Boolean)

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

def glob?
  !path.spec.grep(Nodes::Star).empty?
end

#ipObject


159
160
161
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 159

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

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 141

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


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

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

#required_default?(key) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 123

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

#required_defaultsObject


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

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

#required_keysObject


96
97
98
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 96

def required_keys
  required_parts + required_defaults.keys
end

#required_partsObject


119
120
121
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 119

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

#requirementsObject


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

def requirements  # needed for rails `rails routes`

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

#requires_matching_verb?Boolean

Returns:

  • (Boolean)

163
164
165
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 163

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

#score(constraints) ⇒ Object


100
101
102
103
104
105
106
107
108
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 100

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


92
93
94
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 92

def segments
  path.names
end

#verbObject


167
168
169
# File 'actionpack/lib/action_dispatch/journey/route.rb', line 167

def verb
  verbs.join('|')
end