Class: Reins::RouteObject
Instance Method Summary collapse
- #check_url(url) ⇒ Object
- #get_dest(dest, routing_params = {}) ⇒ Object
-
#initialize ⇒ RouteObject
constructor
A new instance of RouteObject.
- #match(url, *args) ⇒ Object
Constructor Details
#initialize ⇒ RouteObject
Returns a new instance of RouteObject.
3 4 5 |
# File 'lib/reins/routing.rb', line 3 def initialize @rules = [] end |
Instance Method Details
#check_url(url) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/reins/routing.rb', line 41 def check_url(url) @rules.each do |r| m = r[:regexp].match(url) if m = r[:options] params = [:default].dup r[:vars].each_with_index do |v, i| params[v] = m.captures[i] end # TODO: remove dest = nil in GH code if r[:dest] return get_dest(r[:dest], params) else controller = params["controller"] action = params["action"] return get_dest("#{controller}" + "##{action}", params) end end end nil end |
#get_dest(dest, routing_params = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/reins/routing.rb', line 65 def get_dest(dest, routing_params = {}) return dest if dest.respond_to?(:call) if dest =~ /^([^#]+)#([^#]+)$/ name = $1.capitalize con = Object.const_get("#{name}Controller") return con.action($2, routing_params) end raise "No destination: #{dest.inspect}!" end |
#match(url, *args) ⇒ Object
7 8 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 |
# File 'lib/reins/routing.rb', line 7 def match(url, *args) = {} = args.pop if args[-1].is_a?(Hash) [:default] ||= {} dest = nil dest = args.pop if args.size > 0 raise "Too many args!" if args.size > 0 parts = url.split("/") parts.select! { |p| !p.empty? } vars = [] regexp_parts = parts.map do |part| if part[0] == ":" vars << part[1..-1] "([a-zA-Z0-9]+)" elsif part[0] == "*" vars << part[1..-1] "(.*)" else part end end regexp = regexp_parts.join("/") @rules.push({ :regexp => Regexp.new("^/#{regexp}$"), :vars => vars, :dest => dest, :options => , }) end |