Class: Railsless::ActiveRecord::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/railsless/active_record/root.rb

Class Method Summary collapse

Class Method Details

.calculate(flag_file = nil, start_from = nil) ⇒ Object



11
12
13
14
15
16
# 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) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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