Class: ActionController::Routing::Route
- Defined in:
- lib/action_controller/routing/route.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#optimise ⇒ Object
Returns the value of attribute optimise.
-
#requirements ⇒ Object
Returns the value of attribute requirements.
-
#segments ⇒ Object
Returns the value of attribute segments.
Instance Method Summary collapse
-
#build_query_string(hash, only_keys = nil) ⇒ Object
Build a query string from the keys of the given hash.
-
#defaults ⇒ Object
Return a hash of key/value pairs representing the keys in the route that have defaults, or which are specified by non-regexp requirements.
-
#freeze ⇒ Object
TODO: Route should be prepared and frozen on initialize.
-
#initialize(segments = [], requirements = {}, conditions = {}) ⇒ Route
constructor
A new instance of Route.
- #matches_controller_and_action?(controller, action) ⇒ Boolean
-
#optimise? ⇒ Boolean
Indicates whether the routes should be optimised with the string interpolation version of the named routes methods.
-
#parameter_shell ⇒ Object
A route’s parameter shell contains parameter values that are not in the route’s path, but should be placed in the recognized hash.
- #segment_keys ⇒ Object
-
#significant_keys ⇒ Object
Return an array containing all the keys that are used in this route.
- #to_s ⇒ Object
Constructor Details
#initialize(segments = [], requirements = {}, conditions = {}) ⇒ Route
Returns a new instance of Route.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/action_controller/routing/route.rb', line 6 def initialize(segments = [], requirements = {}, conditions = {}) @segments = segments @requirements = requirements @conditions = conditions if !significant_keys.include?(:action) && !requirements[:action] @requirements[:action] = "index" @significant_keys << :action end # Routes cannot use the current string interpolation method # if there are user-supplied <tt>:requirements</tt> as the interpolation # code won't raise RoutingErrors when generating has_requirements = @segments.detect { |segment| segment.respond_to?(:regexp) && segment.regexp } if has_requirements || @requirements.keys.to_set != Routing::ALLOWED_REQUIREMENTS_FOR_OPTIMISATION @optimise = false else @optimise = true end end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
4 5 6 |
# File 'lib/action_controller/routing/route.rb', line 4 def conditions @conditions end |
#optimise ⇒ Object
Returns the value of attribute optimise.
4 5 6 |
# File 'lib/action_controller/routing/route.rb', line 4 def optimise @optimise end |
#requirements ⇒ Object
Returns the value of attribute requirements.
4 5 6 |
# File 'lib/action_controller/routing/route.rb', line 4 def requirements @requirements end |
#segments ⇒ Object
Returns the value of attribute segments.
4 5 6 |
# File 'lib/action_controller/routing/route.rb', line 4 def segments @segments end |
Instance Method Details
#build_query_string(hash, only_keys = nil) ⇒ Object
Build a query string from the keys of the given hash. If only_keys
is given (as an array), only the keys indicated will be used to build the query string. The query string will correctly build array parameter values.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/action_controller/routing/route.rb', line 43 def build_query_string(hash, only_keys = nil) elements = [] (only_keys || hash.keys).each do |key| if value = hash[key] elements << value.to_query(key) end end elements.empty? ? '' : "?#{elements.sort * '&'}" end |
#defaults ⇒ Object
Return a hash of key/value pairs representing the keys in the route that have defaults, or which are specified by non-regexp requirements.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/action_controller/routing/route.rb', line 83 def defaults @defaults ||= returning({}) do |hash| segments.each do |segment| next unless segment.respond_to? :default hash[segment.key] = segment.default unless segment.default.nil? end requirements.each do |key,req| next if Regexp === req || req.nil? hash[key] = req end end end |
#freeze ⇒ Object
TODO: Route should be prepared and frozen on initialize
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/action_controller/routing/route.rb', line 110 def freeze unless frozen? write_generation! write_recognition! prepare_matching! parameter_shell significant_keys defaults to_s end super end |
#matches_controller_and_action?(controller, action) ⇒ Boolean
96 97 98 99 100 |
# File 'lib/action_controller/routing/route.rb', line 96 def matches_controller_and_action?(controller, action) prepare_matching! (@controller_requirement.nil? || @controller_requirement === controller) && (@action_requirement.nil? || @action_requirement === action) end |
#optimise? ⇒ Boolean
Indicates whether the routes should be optimised with the string interpolation version of the named routes methods.
29 30 31 |
# File 'lib/action_controller/routing/route.rb', line 29 def optimise? @optimise && ActionController::Base::optimise_named_routes end |
#parameter_shell ⇒ Object
A route’s parameter shell contains parameter values that are not in the route’s path, but should be placed in the recognized hash.
For example, +=> ‘pages’, :action => ‘show’ is the shell for the route:
map.connect '/page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
62 63 64 65 66 67 68 |
# File 'lib/action_controller/routing/route.rb', line 62 def parameter_shell @parameter_shell ||= returning({}) do |shell| requirements.each do |key, requirement| shell[key] = requirement unless requirement.is_a? Regexp end end end |
#segment_keys ⇒ Object
33 34 35 36 37 |
# File 'lib/action_controller/routing/route.rb', line 33 def segment_keys segments.collect do |segment| segment.key if segment.respond_to? :key end.compact end |
#significant_keys ⇒ Object
Return an array containing all the keys that are used in this route. This includes keys that appear inside the path, and keys that have requirements placed upon them.
73 74 75 76 77 78 79 |
# File 'lib/action_controller/routing/route.rb', line 73 def significant_keys @significant_keys ||= returning([]) do |sk| segments.each { |segment| sk << segment.key if segment.respond_to? :key } sk.concat requirements.keys sk.uniq! end end |
#to_s ⇒ Object
102 103 104 105 106 107 |
# File 'lib/action_controller/routing/route.rb', line 102 def to_s @to_s ||= begin segs = segments.inject("") { |str,s| str << s.to_s } "%-6s %-40s %s" % [(conditions[:method] || :any).to_s.upcase, segs, requirements.inspect] end end |