Class: GitSwap::Swapper

Inherits:
Object
  • Object
show all
Defined in:
lib/git_swap/swapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Swapper

Returns a new instance of Swapper.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/git_swap/swapper.rb', line 7

def initialize(args)
  raise ArgumentError unless args.is_a? Array
  @options = GitSwap::Options.new(args)
  @config = GitSwap::Config.new(args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Use method_missing for delegation Original ActiveSupport version delegate :usage?, :config?, :edit?, :list?, :version?, :global?, to: :options delegate :profile, :name, :username, :email, :ssh, :ssh_command, :print_list, :configure!, :edit!, :valid_profile?, to: :config



70
71
72
73
74
75
76
77
78
# File 'lib/git_swap/swapper.rb', line 70

def method_missing(method, *args)
  if @options.respond_to?(method)
    @options.send(method, *args)
  elsif @config.respond_to?(method)
    @config.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/git_swap/swapper.rb', line 5

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/git_swap/swapper.rb', line 5

def options
  @options
end

Instance Method Details

#git_repo?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/git_swap/swapper.rb', line 30

def git_repo?
  if GitHelper.git_repo? || 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


55
56
57
58
59
60
61
62
63
64
# File 'lib/git_swap/swapper.rb', line 55

def print_settings
  if options.verbose?
    puts "\nGit Config:"
    puts `git config #{git_config_flag} -l --show-origin | grep user`
    puts "\nSSH:"
    puts `ssh-add -l`
  else
    puts "Swapped to profile #{profile}"
  end
end


47
48
49
# File 'lib/git_swap/swapper.rb', line 47

def print_usage
  puts usage
end


51
52
53
# File 'lib/git_swap/swapper.rb', line 51

def print_version
  puts GitSwap::VERSION
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/git_swap/swapper.rb', line 13

def run
  return unless options.valid_args?
  if usage?
    print_usage
  elsif config?
    configure!
  elsif edit?
    edit!
  elsif list?
    print_list
  elsif version?
    print_version
  else
    set!
  end
end

#set!Object



39
40
41
42
43
44
45
# File 'lib/git_swap/swapper.rb', line 39

def set!
  return unless valid_profile? && git_repo?

  set_git_config
  set_ssh
  print_settings
end