Module: RareMap::RailsLocator

Included in:
Mapper
Defined in:
lib/rare_map/rails_locator.rb

Overview

RareMap::RailsLocator locates the root of a rails application from any where inside it.

Author:

  • Wei-Ming Wu

Instance Method Summary collapse

Instance Method Details

#locate_rails_root(path = '.', depth = 5) ⇒ String?

Finds the root of a rails application.

Parameters:

  • path (String) (defaults to: '.')

    the location where start to search

  • depth (Integer) (defaults to: 5)

    the max levels of folders to search

Returns:

  • (String, nil)

    the root of a rails application



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rare_map/rails_locator.rb', line 11

def locate_rails_root(path = '.', depth = 5)
  rails_dirs = %w(app config db lib log public)
  
  depth.times do |level|
    found = true
    paths = [path]
    
    level.times { paths << '..' }
    
    rails_dirs.each do |dir|
      found = false unless Dir.exist? File.join(*(paths + [dir]))
    end
    
    return File.absolute_path(File.join(*paths)) if found
  end
  
  nil
end