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
# File 'lib/action_dispatch/journey/route.rb', line 14

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

  @constraints = constraints
  @defaults    = defaults
  @required_defaults = nil
  @required_parts    = nil
  @parts             = nil
  @decorated_ast     = nil
  @precedence        = 0
  @path_formatter    = @path.build_formatter
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



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

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)


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

def dispatcher?
  @app.dispatcher?
end

#format(path_options) ⇒ Object



67
68
69
# File 'lib/action_dispatch/journey/route.rb', line 67

def format(path_options)
  @path_formatter.evaluate path_options
end

#glob?Boolean

Returns:

  • (Boolean)


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

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

#ipObject



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

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

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/action_dispatch/journey/route.rb', line 97

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

#optional_partsObject



71
72
73
# File 'lib/action_dispatch/journey/route.rb', line 71

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

#partsObject Also known as: segment_keys



62
63
64
# File 'lib/action_dispatch/journey/route.rb', line 62

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

#required_default?(key) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/action_dispatch/journey/route.rb', line 79

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

#required_defaultsObject



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

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

#required_keysObject



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

def required_keys
  required_parts + required_defaults.keys
end

#required_partsObject



75
76
77
# File 'lib/action_dispatch/journey/route.rb', line 75

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

#requirementsObject

:nodoc:



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

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

#score(constraints) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/action_dispatch/journey/route.rb', line 52

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



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

def segments
  path.names
end

#verbObject



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

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