Class: GitSwitch::Switcher
- Inherits:
-
Object
- Object
- GitSwitch::Switcher
- Defined in:
- lib/git_switch/switcher.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
- #get_profile(args) ⇒ Object
- #git_repo? ⇒ Boolean
- #global? ⇒ Boolean
-
#initialize(args) ⇒ Switcher
constructor
A new instance of Switcher.
- #list? ⇒ Boolean
- #print_list ⇒ Object
- #run ⇒ Object
- #set! ⇒ Object
- #valid_profile? ⇒ Boolean
Constructor Details
#initialize(args) ⇒ Switcher
Returns a new instance of Switcher.
8 9 10 11 12 13 |
# File 'lib/git_switch/switcher.rb', line 8 def initialize(args) raise ArgumentError unless args.is_a? Array @config = load_config = GitSwitch::Options.new(args) @profile = get_profile(args) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
6 7 8 |
# File 'lib/git_switch/switcher.rb', line 6 def profile @profile end |
Instance Method Details
#get_profile(args) ⇒ Object
27 28 29 |
# File 'lib/git_switch/switcher.rb', line 27 def get_profile(args) args.detect {|a| !a.start_with? '-'} end |
#git_repo? ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/git_switch/switcher.rb', line 40 def git_repo? if GitHelper.git_repo? return true else if global? return true else puts "Not a git repo. Please run from a git repo or run with `-g` to update global settings." return false end end end |
#global? ⇒ Boolean
23 24 25 |
# File 'lib/git_switch/switcher.rb', line 23 def global? .global? end |
#list? ⇒ Boolean
19 20 21 |
# File 'lib/git_switch/switcher.rb', line 19 def list? .list? end |
#print_list ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/git_switch/switcher.rb', line 63 def print_list profiles = config.map do |key, value| prefix = value["username"] == current_git_username ? "=>" : " " "#{prefix} #{key}" end puts profiles puts "\n# => - current" if config.any? {|key, value| value["username"] == current_git_username} end |
#run ⇒ Object
15 16 17 |
# File 'lib/git_switch/switcher.rb', line 15 def run list? ? print_list : set! end |
#set! ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/git_switch/switcher.rb', line 53 def set! return unless .valid_args? && valid_profile? return unless git_repo? flag = global? ? '--global' : '' set_git_config(flag) set_ssh end |
#valid_profile? ⇒ Boolean
31 32 33 34 35 36 37 38 |
# File 'lib/git_switch/switcher.rb', line 31 def valid_profile? if config.has_key?(profile) return true else puts "Profile '#{profile}' not found!" return false end end |