lib/pgit/current_project.rb in pgit-0.0.4 vs lib/pgit/current_project.rb in pgit-1.0.0

- old
+ new

@@ -1,56 +1,20 @@ -# -# 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 -# +require 'pgit' +require 'forwardable' module PGit class CurrentProject - def initialize(config_yaml) - @current_project = find_current_project(config_yaml) - end + extend Forwardable - def pwd - project_path = @current_project["path"] - File.expand_path(project_path, __FILE__) + PGit::Project.instance_methods(false).each do |m| + def_delegator :@current, m 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}/) + def initialize(configuration) + @current = configuration.projects.find do |p| + File.expand_path(p.path) == Dir.pwd 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 + raise PGit::Error::User, "Current Project does not exist. See `pgit proj add -h`" unless @current end end end