Class: GitSwitch::Switcher

Inherits:
Object
  • Object
show all
Defined in:
lib/git_switch/switcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Switcher

Your code goes here…

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/git_switch/switcher.rb', line 10

def initialize(args)
  raise ArgumentError unless args.is_a? Array
  @config = YAML.load_file(File.expand_path('~/.gitswitch'))
  @global = check_global(args)
  @profile = get_profile(args)
  @valid = valid_args?(args)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/git_switch/switcher.rb', line 8

def config
  @config
end

#globalObject (readonly)

Returns the value of attribute global.



8
9
10
# File 'lib/git_switch/switcher.rb', line 8

def global
  @global
end

#profileObject (readonly)

Returns the value of attribute profile.



8
9
10
# File 'lib/git_switch/switcher.rb', line 8

def profile
  @profile
end

#validObject (readonly)

Returns the value of attribute valid.



8
9
10
# File 'lib/git_switch/switcher.rb', line 8

def valid
  @valid
end

Instance Method Details

#check_global(args) ⇒ Object



18
19
20
# File 'lib/git_switch/switcher.rb', line 18

def check_global(args)
  (args.include? '-g') || (args.include? '--global')
end

#get_profile(args) ⇒ Object



22
23
24
25
26
# File 'lib/git_switch/switcher.rb', line 22

def get_profile(args)
  # TODO: RCR - Verify profile exists in config file
  # TODO: RCR - Handle missing or empty config file
  args.detect {|a| !a.start_with? '-'}
end

#no_flags?(args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/git_switch/switcher.rb', line 32

def no_flags?(args)
  args.length == 1 && args.count {|a| a.start_with? '-'} == 0
end

#one_flag?(args) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/git_switch/switcher.rb', line 36

def one_flag?(args)
  args.length == 2 && args.count {|a| a.start_with?('-')} == 1
end

#set!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git_switch/switcher.rb', line 40

def set!
  return unless valid
  flag = global ? '--global' : ''

  puts "\nGit Config:"
  `git config #{flag} user.name "#{name}"`
  `git config #{flag} user.username "#{username}"`
  `git config #{flag} user.email "#{email}"`
  puts `git config #{flag} -l --show-origin | grep user`

  puts "\nSSH:"
  `ssh-add -D`
  `ssh-add #{ssh}`
  puts `ssh-add -l`
end

#valid_args?(args) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/git_switch/switcher.rb', line 28

def valid_args?(args)
  no_flags?(args) || one_flag?(args)
end