class GitSu::Switcher
Public Class Methods
new(config_repository, git, user_list, output)
click to toggle source
# File lib/gitsu/switcher.rb, line 21 def initialize(config_repository, git, user_list, output) @config_repository, @git, @user_list, @output = config_repository, git, user_list, output end
Public Instance Methods
add(user_string)
click to toggle source
# File lib/gitsu/switcher.rb, line 76 def add(user_string) begin user = User.parse(user_string) rescue User::ParseError => parse_error @output.puts parse_error.message return end add_parsed_user user end
clear(*scopes)
click to toggle source
# File lib/gitsu/switcher.rb, line 57 def clear(*scopes) scope_list = scopes.to_sentence if scopes.include? :all scopes = [:local, :global, :system] end @output.puts "Clearing Git #{scopes.pluralize('user')} in #{scope_list} #{scopes.pluralize('scope')}" scopes.each do |scope| @git.clear_user(scope) end end
edit_config()
click to toggle source
# File lib/gitsu/switcher.rb, line 53 def edit_config @git.edit_gitsu_config end
list()
click to toggle source
# File lib/gitsu/switcher.rb, line 70 def list @user_list.list.each do |user| @output.puts @git.render(user) end end
print_current(*scopes)
click to toggle source
# File lib/gitsu/switcher.rb, line 37 def print_current(*scopes) if scopes.include? :all @output.puts "Current user: #{@git.render_user(:derived)}" @output.puts @output.puts "Local: #{@git.render_user(:local)}" @output.puts "Global: #{@git.render_user(:global)}" @output.puts "System: #{@git.render_user(:system)}" else scopes.each do |scope| unless @git.selected_user(scope).none? @output.puts @git.render_user(scope) end end end end
request(scope, *user_strings)
click to toggle source
# File lib/gitsu/switcher.rb, line 25 def request(scope, *user_strings) begin parsed, not_parsed = try_to_parse_all user_strings found = find_all not_parsed combined_user = combine_all(parsed + found) select_user combined_user, scope parsed.each {|user| add_parsed_user(user) } rescue RuntimeError => error @output.puts error.message end end
Private Instance Methods
add_parsed_user(user)
click to toggle source
# File lib/gitsu/switcher.rb, line 100 def add_parsed_user(user) if @user_list.list.include? user @output.puts "User '#{user}' already in user list (try switching to them with 'git su #{user.initials}')" else @user_list.add user @output.puts "User '#{user}' added to users" end end
combine_all(users)
click to toggle source
# File lib/gitsu/switcher.rb, line 117 def combine_all(users) group_email = @config_repository.group_email_address users.inject(User::NONE) do |combined_user, user| combined_user.combine user, group_email end end
find_all(user_strings)
click to toggle source
# File lib/gitsu/switcher.rb, line 109 def find_all(user_strings) if user_strings.empty? [] else @user_list.find *user_strings end end
select_user(user, scope)
click to toggle source
# File lib/gitsu/switcher.rb, line 124 def select_user(user, scope) if scope == :default scope = @config_repository.default_select_scope end @git.select_user(user, scope) @output.puts "Switched #{scope} user to #{@git.render user}" end
try_to_parse_all(user_strings)
click to toggle source
# File lib/gitsu/switcher.rb, line 87 def try_to_parse_all(user_strings) parsed = [] not_parsed = [] user_strings.each do |user_string| begin parsed << User.parse(user_string) rescue User::ParseError => parse_error not_parsed << user_string end end return [parsed, not_parsed] end