Class: Egalite::Route
Class Method Summary collapse
Instance Method Summary collapse
- #escape(s) ⇒ Object
- #get_path_and_params_from_params(params, current_host = nil, current_port = nil, current_scheme = nil) ⇒ Object
-
#initialize(route_def) ⇒ Route
constructor
A new instance of Route.
- #link_to(title, params, host = nil, port = nil, scheme = nil) ⇒ Object
- #match(path) ⇒ Object
- #url_for(params, host = nil, port = nil, scheme = nil) ⇒ Object
Constructor Details
#initialize(route_def) ⇒ Route
Returns a new instance of Route.
3 4 5 6 7 8 |
# File 'lib/egalite/route.rb', line 3 def initialize(route_def) @route = route_def @controller = nil @prefix = nil @action = nil end |
Class Method Details
.default_routes ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/egalite/route.rb', line 9 def self.default_routes routes = [] routes << Route.new([ [:controller], [:controller], [:action], [:param_arg, :id], [:params] ]) routes << Route.new([ [:controller], [:controller], [:param_arg, :id], [:params] ]) routes << Route.new([ [:controller], [:action], [:param_arg, :id], [:params] ]) routes << Route.new([ [:controller], [:param_arg, :id], [:params] ]) routes << Route.new([ [:action], [:param_arg, :id], [:params] ]) routes << Route.new([ [:param_arg, :id], [:params] ]) routes end |
Instance Method Details
#escape(s) ⇒ Object
107 108 109 |
# File 'lib/egalite/route.rb', line 107 def escape(s) Rack::Utils.escape(s) end |
#get_path_and_params_from_params(params, current_host = nil, current_port = nil, current_scheme = nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/egalite/route.rb', line 111 def get_path_and_params_from_params(params, current_host = nil, current_port = nil, current_scheme = nil) route = @route || [] pathary = [] controller_exist = false action_exist = false contfrags = (@controller || "").to_s.split('/') scheme = nil host = nil port = nil if params[:scheme] scheme = params[:scheme].to_s params.delete(:scheme) end if params[:host] host = params[:host] params.delete(:host) end if params[:port] port = params[:port].to_i params.delete(:port) end if (scheme or port or host) raise "get_path_and_params_from_params: current_host is not supplied." unless host or current_host scheme = scheme || current_scheme || 'http' prefix = "#{scheme}://#{host || current_host}" unless (current_scheme == 'http' and current_port == 80) or (current_scheme == 'https' and current_port == 443) port ||= current_port end if port unless (scheme == 'http' and port == 80) or (scheme == 'https' and port == 443) prefix << ":#{port}" end end end route.each { |fragment| command = fragment[0] case command when :controller next if controller_exist if params[:controller] == nil pathary += contfrags elsif params[:controller] =~ /^\// pathary += params[:controller].to_s.split('/') else pathary += contfrags[0..-2] pathary += params[:controller].to_s.split('/') end controller_exist = pathary.size params.delete(:controller) when :action pathary << (params[:action] || (controller_exist ? nil : @action)) action_exist = true params.delete(:action) when :param pathary << params[fragment[1]] params.delete(fragment[1]) when :param_arg pathary << params[fragment[1]] params.delete(fragment[1]) when :param_fix value = params[fragment[1]] value = fragment[2] unless value pathary << params[fragment[1]] params.delete(fragment[1]) when :controller_fix next when :params ary = (params[:params] || []) if ary.respond_to?(:map) pathary += ary.map { |s| s.to_s } else pathary += [ary.to_s] end params.delete(:params) end } if not action_exist and params[:action] if controller_exist pathary.insert(controller_exist,params[:action]) else pathary.unshift(params[:action]) end params.delete(:action) end if not controller_exist and params[:controller] pathary.unshift(params[:controller].to_s.split('/')).flatten! params.delete(:controller) end pathary = pathary.compact.map { |frag| escape(frag) } path = "/" + pathary.join('/').sub(/\/+$/,'').sub(/^\//,'') [path, params, pathary, prefix] end |
#link_to(title, params, host = nil, port = nil, scheme = nil) ⇒ Object
227 228 229 |
# File 'lib/egalite/route.rb', line 227 def link_to(title, params, host = nil, port = nil, scheme = nil) "<a href='#{url_for(params, host, port, scheme)}'>#{title}</a>" end |
#match(path) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/egalite/route.rb', line 48 def match(path) path = path.sub(/^\/+/,'') pathary = path.to_s.split('/') controller = nil action = nil path_params = [] params = {} prefix = [] @route.each { |fragment| command = fragment[0] case command when :controller if pathary.empty? and controller controller += '/index' elsif pathary.empty? return nil else controller += '/' if controller controller ||= '' controller += pathary.shift end when :action return nil if pathary.empty? action = pathary.shift when :param next if pathary.empty? val = pathary.shift params[fragment[1]] = val prefix << val unless controller when :param_arg next if pathary.empty? val = pathary.shift params[fragment[1]] = val if fragment[1] path_params << val prefix << val unless controller when :param_fix return nil if pathary[0] != fragment[2] params[fragment[1]] = pathary.shift prefix << val unless controller when :controller_fix return nil if pathary[0] != fragment[1] controller += '/' if controller controller ||= '' controller += pathary.shift when :params path_params += pathary pathary = [] end } return nil if pathary.size > 0 @controller = controller @action = action @path_params = path_params @params = params @prefix = prefix.join('/') [controller, action, path_params, params] end |
#url_for(params, host = nil, port = nil, scheme = nil) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/egalite/route.rb', line 208 def url_for(params, host = nil, port = nil, scheme = nil) (path, params, z, prefix) = get_path_and_params_from_params(params, host, port, scheme) if params and params.size > 0 q = [] params.each { |k,v| if v.is_a?(Hash) v.each { |k2,v2| next if v2 == nil q << "#{escape(k)}[#{escape(k2)}]=#{escape(v2)}" } else q << "#{escape(k)}=#{escape(v)}" if v end } path += "?" + q.join('&') unless q.empty? end "#{prefix}#{path}" end |