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.
- #generate(options, hash, expire_on = {}) ⇒ Object
- #generate_extras(options, hash, expire_on = {}) ⇒ Object
-
#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.
- #required_segment_keys ⇒ Object
- #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.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/action_controller/routing/route.rb', line 48 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.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/action_controller/routing/route.rb', line 88 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/action_controller/routing/route.rb', line 115 def freeze unless frozen? write_generation! write_recognition! prepare_matching! parameter_shell significant_keys defaults to_s end super end |
#generate(options, hash, expire_on = {}) ⇒ Object
130 131 132 133 |
# File 'lib/action_controller/routing/route.rb', line 130 def generate(, hash, expire_on = {}) path, hash = generate_raw(, hash, expire_on) append_query_string(path, hash, extra_keys()) end |
#generate_extras(options, hash, expire_on = {}) ⇒ Object
135 136 137 138 |
# File 'lib/action_controller/routing/route.rb', line 135 def generate_extras(, hash, expire_on = {}) path, hash = generate_raw(, hash, expire_on) [path, extra_keys()] end |
#matches_controller_and_action?(controller, action) ⇒ Boolean
101 102 103 104 105 |
# File 'lib/action_controller/routing/route.rb', line 101 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+/
67 68 69 70 71 72 73 |
# File 'lib/action_controller/routing/route.rb', line 67 def parameter_shell @parameter_shell ||= returning({}) do |shell| requirements.each do |key, requirement| shell[key] = requirement unless requirement.is_a? Regexp end end end |
#required_segment_keys ⇒ Object
39 40 41 42 |
# File 'lib/action_controller/routing/route.rb', line 39 def required_segment_keys required_segments = segments.select {|seg| (!seg.optional? && !seg.is_a?(DividerSegment)) || seg.is_a?(PathSegment) } required_segments.collect { |seg| seg.key if seg.respond_to?(:key)}.compact 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.
78 79 80 81 82 83 84 |
# File 'lib/action_controller/routing/route.rb', line 78 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
107 108 109 110 111 112 |
# File 'lib/action_controller/routing/route.rb', line 107 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 |