2012-08-03 3 views
0

레이크 라우트 배열이 있는데 route.conditions [: request_method]가있는 모든 라우트를 GET으로 추출하려고합니다.'GET'메서드가있는 모든 레이크 라우트 수집

문제 :

:request_method is a regex (:request_method=>/^GET$/) 

> routes.select { |route| route.conditions[:request_method] == /GET/ } 
> [] 

내 선택이 옳았다 생각. 이것은 작동하고, 모든 경로 방법 출력 :

> routes.each { |route| print route.conditions[:request_method] } 
> {:request_method=>/^GET$/}{:request_method=>/^GET$/}{:request_method=>/^PUT$/}{:request_method=>/^GET$/}{:request_method=>/^PUT$/}{:request_method=>/^POST$/}{:request_method=>/^GET$/}{:request_method=>/^GET$/} 

모든 아이디어는 내가 이것을 달성하는 방법을?

답변

0

내 대답을 찾았습니다. routes.each는 여행 객체를 반환, 그래서 내가 먼저 내 자신의 사용자 정의 해시를 생성 :

routes = Rails.application.routes.routes.to_a 
routes = routes.collect { |route| {name: route.name, method: route.verb} } 
routes = routes.select { |route| route[:method] == /^GET$/ } 

나는 그래서 변화에 열려있어이 더 우아하게 재 작업 할 수 있습니다 확신합니다. 그것은 작동합니다. 이것이 가장 중요한 부분입니다.