莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理
              今天在javaeye論壇上看到有人有這個需求,順手寫了下。原理就是通過遍歷Controllers目錄,并用正則表達式取出Controller名和它所有的action。

              @controllers=Hash.new
              path
          ="#{RAILS_ROOT}/app/controllers/"
              Dir
          .new(path).entries.each do |f|
                
          if !f.index('.rb').nil? and f.index('.rb')>0
                  
                  controller
          =File.open(path+f.to_s)
                  s
          =controller.read
                   
          /class\s(.*)\s\</.match(s)
                  controller_name
          =$1.to_s
                  actions
          =[]
                  s
          .scan(/def\s(.*)\s/).each|action| actions<<(action[0]) }
                  
          @controllers[controller_name]=actions
                  controller
          .close
                end
              end
              
              
          @controllers.each_pair do |name, actions|
                actions
          .each do |action| 
                  puts 
          |name<<" "<<action
                end
              end


          主站蜘蛛池模板: 象山县| 辽阳县| 陈巴尔虎旗| 许昌市| 福建省| 漾濞| 得荣县| 宁武县| 华坪县| 蓬莱市| 平昌县| 淮阳县| 沾化县| 平乡县| 汾西县| 土默特左旗| 江陵县| 灌阳县| 合山市| 兖州市| 济宁市| 乐陵市| 汝南县| 清河县| 兴安县| 洮南市| 辽阳市| 三原县| 无为县| 称多县| 西平县| 奇台县| 长岛县| 旬邑县| 临城县| 岳西县| 高陵县| 襄汾县| 郯城县| 临沂市| 阳谷县|