Sha256: e7147e323c2cdd7b81157c291af923739c5d078dc7ac82e8d9e108b3e5024c85

Contents?: true

Size: 876 Bytes

Versions: 2

Compression:

Stored size: 876 Bytes

Contents

module Quandl
  module ProjectRoot
    # Borrowed from https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/ruby_project.rb

    def self.root
      @project_root ||= determine_root
    end

    def self.determine_root
      find_first_parent_containing('lib') || '.'
    end

    def self.find_first_parent_containing(dir)
      ascend_until { |path| File.exist?(File.join(path, dir)) }
    end

    def self.ascend_until
      fs = File::SEPARATOR
      escaped_slash = "\\#{fs}"
      special = "_ESCAPED_SLASH_"
      project_path = File.expand_path(".")
      parts = project_path.gsub(escaped_slash, special).squeeze(fs).split(fs).map do |x|
        x.gsub(special, escaped_slash)
      end

      until parts.empty?
        path = parts.join(fs)
        path = fs if path == ""
        return path if yield(path)
        parts.pop
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quandl-config-0.0.3 lib/quandl/project_root.rb
quandl-config-0.0.2 lib/quandl/project_root.rb