Module: AlpacaComplete::Detect

Defined in:
lib/AlpacaComplete/detect.rb

Class Method Summary collapse

Class Method Details

.detect(filename) ⇒ Object

program portability from rails.vim return rails root path



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/AlpacaComplete/detect.rb', line 5

def self.detect(  filename )
  fn = filename.gsub('\c^file://', '')

  if File.readable?("#{fn}/config/environment.rb")
    return fn
  else
    fn = File.dirname(fn)
  end

  # return rails_root_path if filename is config/environment.rb
  if fn =~ /[\/]config[\/]environment\.rb$/
    return fn[0, fn.length - 22]
  end

  ofn = nil
  until fn == ofn || fn == nil do
    return fn if File.readable?("#{fn}/config/environment.rb")

    ofn = fn
    rails_path = ofn.match( /^(.*)[\/](app|config|db|doc|extras|features|lib|log|public|script|spec|stories|test|tmp|vendor|publics|sdoc)($|[\/].*$)/)

    if rails_path
      fn = rails_path[1]
    else
      break
    end
  end

  return nil
end