class Railsless::ActiveRecord::Root

Public Class Methods

calculate(flag_file=nil, start_from=nil) click to toggle source
# File lib/railsless/active_record/root.rb, line 11
def self.calculate(flag_file=nil, start_from=nil)
  find_root_with_flag(
    flag_file  || 'config.ru', # Look for something signifying Rack. Not much else to go on.
    start_from || Dir.pwd # Yes, Rails does this.
  )
end
find_root_with_flag(flag, starting_path) click to toggle source

Starts from a directory, and works upwards looking for a particular file.

# File lib/railsless/active_record/root.rb, line 19
def self.find_root_with_flag(flag, starting_path)
  # This process if only useful if the starting_path is an absolute path.
  path = if starting_path[0] == '/'
           starting_path
         else
           File.absolute_path(starting_path) # Relative; turn into absolute path
         end

  while path && File.directory?(path) && !File.exist?("#{path}/#{flag}")
    parent = File.dirname(path)
    path = (parent != path) && parent
  end

  if path && File.exist?("#{path}/#{flag}")
    File.realpath path
  else
    raise "Could not find root path for hosting application"
  end
end