Sha256: 992a2dea2c470dda177887887bbc1338ae2a246eb7578753796db406f9f522d7

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

#
#   Decides what the "current project" is, in relation to the pwd.
#
# - config_yaml: has the project configurations. It has at least one
#   project. Each project has an (pivotal) api_token, path, and (pivotal)
#   id
#

module PGit
  class CurrentProject
    def initialize(config_yaml)
      @current_project = find_current_project(config_yaml)
    end

    def pwd
      project_path = @current_project["path"]
      File.expand_path(project_path, __FILE__)
    end

    def id
      @current_project["id"]
    end

    def api_token
      @current_project["api_token"]
    end

    private

    def escape_slashes(project_path)
      project_path.gsub('/','\/')
    end

    def find_matching_projects(projects)
      projects.select do |project|
        project_path = project["path"]
        extended_path = File.expand_path(project_path, __FILE__)
        escaped_project = escape_slashes(extended_path)
        Dir.pwd.match(/#{escaped_project}/)
      end
    end

    def find_current_project(config_yaml)
      projects = config_yaml["projects"]
      matching_projects = find_matching_projects(projects)

      PGit::CurrentProject::Validator.new(matching_projects)
      find_best_match(matching_projects)
    end

    def find_best_match(matching_projects)
      matching_projects.sort! { |a,b| b["path"].length <=> a["path"].length }
      matching_projects.first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pgit-0.0.4 lib/pgit/current_project.rb