trivelop @rrrene on

Introducing Homecoming

Today I’m open-sourcing Homecoming, a little gem that let’s you easily traverse all parent directories of a given or the current directory.

This comes in handy when searching for a config file with settings that can be overridden by the same config file in a lower level directory (a popular example being .gitignore).

Install via rubygems

$ gem install homecoming

You can get/browse the source code at http://github.com/rrrene/homecoming

So, how can I use this?

Let’s say you have a directory structure that looks like this:

/
  home/
    rrrene/
      projects/
        your_project/
          .yourconfig
      .yourconfig

and our software wants to find all .yourconfig files in the tree, so that it can load them in succession.

Homecoming.find searches for a given filename in the current and all parent directories:

Homecoming.find(".yourconfig", "/home/rrrene/projects/your_project")
# => ["/home/rrrene/.yourconfig",
#      "/home/rrrene/projects/your_project/.yourconfig"]

If no path is given as second parameter, the current directory is the starting point of the traversal (which is a good default for CLIs).

And if you just want all the parent directories up to the root, you can use Homecoming.each.

Homecoming.each("/home/rrrene/projects/your_project") do |dir|
  # ...
end

It traverses and yields the given and all parent directories, and yields the resulting directories one by one. Again, if no path is given, the current directory is the starting point of the traversal.

In my example, this would yield the following directories beginning with the given/current one:

"/home/rrrene/projects/your_project"
"/home/rrrene/projects"
"/home/rrrene"
"/home"
"/"

Head over to GitHub for the source code. To contribute, just fork it!

Fork me on GitHub