今天在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
![108785.html](https://i-blog.csdnimg.cn/blog_migrate/96919968bef1c59e70a6f0f2f1a5bb3e.jpeg)