Search code examples
ruby-on-railsrubymetaprogrammingcollaboration

Rails: Ruby Code That Can Scan Ruby Code


I'm starting a project with several contributors. We want to keep track of who wrote what code, as well as to get a count of how many methods, controller actions, and views a contributor has written. This necessitates a level of meta-programming that none of us are familiar with.

So far, the best idea we've come up with is to add a comment before each snippet of code with the contributor's username and a short, consistent phrase. For instance:

# method by RacerX
def a_useful_method
  . . .
end

# method by MysteryProgrammer123
def another_useful_method
  . . .
end

# action by MysteryProgrammer123
def new
  . . .
end

Then we'd run a method to count all instances of action by and method by and view by that each user has written throughout the project. Unfortunately, we don't know how to write Ruby code that can inspect other Ruby code. It might not even be possible. If it is possible, though, how is it done?

Alternatively, there might be a better approach that we're not considering.


Solution

  • You should prefer your source control system to track who wrote what. git blame, for instance, can produce an annotated listing showing author and source line.

    Identifying views should be easy, they're in the view directory. Static method definitions can generally be found with regexp /\bdef\s+(?:\w+\.)?(\w+)\b/. Distinguishing "actions" from other methods probably involves filtering method names against common action names and other names discovered by examining your routes.