莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理
              今天在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


          主站蜘蛛池模板: 商河县| 吴江市| 越西县| 新巴尔虎右旗| 桐庐县| 连平县| 绍兴县| 桐梓县| 上饶市| 昌都县| 鄯善县| 铜鼓县| 汶上县| 靖边县| 得荣县| 承德县| 丰台区| 科技| 乐亭县| 左云县| 区。| 邛崃市| 铜陵市| 霍州市| 静宁县| 株洲县| 安远县| 林西县| 会同县| 星子县| 澎湖县| 武定县| 襄垣县| 云霄县| 南充市| 永昌县| 荃湾区| 桓台县| 黄梅县| 隆安县| 格尔木市|