Guest User

Untitled

a guest
Feb 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. module ActionController
  2. module Routing
  3. class RouteSet
  4. attr_accessor :routes, :recognizer
  5.  
  6. def initialize
  7. @routes = []
  8. @route_structure = {}
  9. end
  10.  
  11. def add_named_route(name, path, options = {})
  12. add_route(path, options)
  13. end
  14.  
  15. def add_route(route_string, *args)
  16. @route_structure ||= {}
  17. @routes ||= []
  18.  
  19. params = []
  20. args = (args.pop || {})
  21.  
  22. request_method = args[:conditions] && args[:conditions][:method] ? args[:conditions].delete(:method) : :get
  23.  
  24. requirements = args.delete(:requirements) || {}
  25.  
  26. segments = route_string.split("/").map! do |segment|
  27. segment = Regexp.escape(segment)
  28.  
  29. if segment =~ /:\w+/
  30. segment_symbols = segment.scan(/:(\w+)/).flatten
  31.  
  32. segment_symbols.each do |segment_symbol|
  33. params << segment_symbol.to_sym
  34. requirements[segment_symbol] || ".*"
  35.  
  36. segment.gsub!(/:#{segment_symbol}/, '.*')
  37. end
  38. end
  39.  
  40. segment
  41. end
  42.  
  43. raise "Invalid route: Controller not specified" unless (params.include?(:controller) || args.keys.include?(:controller))
  44.  
  45. new_route = RouterFu::Route.new(segments, params, request_method, args)
  46. @routes << new_route
  47.  
  48. new_route.arguments[:controller] ||= :controller
  49. new_route.arguments[:action] ||= :action
  50.  
  51. @route_structure[request_method] ||= {}
  52. @route_structure[request_method][new_route.arguments[:controller]] ||= {}
  53. @route_structure[request_method][new_route.arguments[:controller]][new_route.arguments[:action]] ||= []
  54. @route_structure[request_method][new_route.arguments[:controller]][new_route.arguments[:action]] << new_route
  55. end
  56.  
  57. def recognize(request)
  58. # path, request_method = :GET
  59. path = request.path
  60. request_method = (request.request_method || :get)
  61.  
  62. @recognizer ||= build_recognizer
  63. RAILS_DEFAULT_LOGGER.error(@recognizer)
  64.  
  65. matched = {}
  66.  
  67. # RAILS_DEFAULT_LOGGER.error("#{request_method} #{path}")
  68.  
  69. captures = @recognizer.match("#{request_method} #{path}").captures[1..-1]
  70.  
  71. # RAILS_DEFAULT_LOGGER.error(@recognizer.pretty_inspect)
  72. # RAILS_DEFAULT_LOGGER.error(@routes.pretty_inspect)
  73. #
  74. # RAILS_DEFAULT_LOGGER.error(@routes[captures.index(captures.compact.last)].pretty_inspect)
  75. if r = @routes[captures.index(captures.compact.last)]
  76. path_segments = path.gsub(/^\//, '').split("/")
  77.  
  78. index = -1
  79. r.segments.each do |segment|
  80. next unless segment == "(.*)" # FIXFXIFXIFXFIX
  81. index += 1
  82.  
  83. matched[r.params[index]] = path_segments[index]
  84. end
  85. matched = r.arguments.merge(matched)
  86. RAILS_DEFAULT_LOGGER.error(matched.pretty_inspect)
  87. matched[:action] = 'index' if matched[:action] == :action
  88. end
  89.  
  90. request.path_parameters = matched
  91. "#{matched[:controller].camelize}Controller".constantize
  92. end
  93.  
  94. def generate(*args)
  95. params = args.pop
  96. request_method = params[:method].to_sym || :get
  97.  
  98. if params.keys.include?(:controller)
  99. controller_routes = @route_structure[:request_method][params[:controller]]
  100.  
  101. unless controller_routes
  102. controller_routes = @route_structure[:request_method][:controller]
  103. end
  104.  
  105. action_routes = controller_routes[(params[:action] || 'index')] || controller_routes[:action]
  106.  
  107. action_routes.each do |route|
  108. if (route.params - params.keys).empty?
  109. puts "generating from #{route.inspect}"
  110. return generate_url(route, params)
  111. else
  112. raise "No route to match that"
  113. end
  114. end
  115. else
  116. raise "No controller provided"
  117. end
  118. end
  119.  
  120. def generate_url(route, params)
  121. route_string = route.segments.join("/")
  122. return route_string unless route_string.include?("(.*)")
  123.  
  124. index = -1
  125. route_string.gsub!(/\(\.\*\)/) do |match|
  126. index += 1
  127. params[route.params[index]]
  128. end
  129. end
  130.  
  131. def route_resources(resources)
  132. resources = resources.to_s
  133. add_route resources, :controller => resources.to_sym, :action => 'index', :method => :get
  134. add_route resources, :controller => resources.to_sym, :action => 'create', :method => :post
  135. add_route "#{resources}/:id", :controller => resources.to_sym, :action => 'show', :method => :get
  136. add_route "#{resources}/:id", :controller => resources.to_sym, :action => 'update', :method => :put
  137. add_route "#{resources}/:id", :controller => resources.to_sym, :action => 'destroy', :method => :delete
  138. add_route "#{resources}/new", :controller => resources.to_sym, :action => 'new', :method => :get
  139. add_route "#{resources}/:id/edit", :controller => resources.to_sym, :action => 'edit', :method => :get
  140. end
  141.  
  142. def build_recognizer
  143. recognizers = []
  144. @routes.each do |route|
  145. recognizers << "(#{route.request_method} #{route.recognizer})"
  146. end
  147.  
  148. @recognizer = /^(#{recognizers.join("|")})$/
  149. end
  150. end
  151. end
  152. end
  153.  
  154. module RouterFu
  155. class Route
  156. attr_accessor :params, :segments, :arguments
  157. attr_reader :recognizer, :request_method
  158.  
  159. def initialize(segment_list, param_list, request_method, argument_list = {})
  160. @segments = segment_list
  161. @params = param_list
  162. @arguments = argument_list || {}
  163. @recognizer = "#{@segments.join("\/")}$"
  164. @request_method = request_method
  165. end
  166. end
  167. end
Add Comment
Please, Sign In to add comment