今天在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
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
- 20:21
- 浏览 (649)
- 评论 (0)
- 分类: ruby & rails
- 相关推荐
发表评论
我的留言簿
-
你好,看过你关于自定义classloader的回帖,想问问几个问题:
  ...
-- by llp20_2000
最近加入圈子
最新评论
-
Ruby Tip——读文件
花花公子 写道IO.read("test.log")嗯,这个方法我是知道的,我前 ...
-- by dennis_zane -
Ruby Tip——读文件
IO.read("test.log")
-- by 花花公子 -
一封邮件
咋我没收到呢....
-- by yangzhihuan -
漂亮的代码
说得比较深.看的时候,觉得句句都正中心坎.看完之后,好像没啥收获.还是要自己试过 ...
-- by yangzhihuan -
广州opensource camp小记
貌似你那件open source camp的T-shirt背后很多广告滴说.
-- by yangzhihuan







评论排行榜