Class: Ayadn::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/ayadn/switch.rb

Instance Method Summary collapse

Constructor Details

#initializeSwitch

Returns a new instance of Switch.



5
6
7
8
9
# File 'lib/ayadn/switch.rb', line 5

def initialize
  @thor = Thor::Shell::Color.new # local statuses
  @status = Status.new # global statuses + utils
  @acc_db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
end

Instance Method Details

#listObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ayadn/switch.rb', line 11

def list
  puts "\n"
  accounts = Databases.all_accounts(@acc_db)
  please if accounts.empty?
  accounts.sort_by! { |acc| acc[0] }
  table = Terminal::Table.new do |t|
    t.style = { :width => 80 }
    t.title = "Ayadn accounts"
    t.headings = ['Username', 'ID', 'Path']
  end
  accounts.each do |acc|
    username = acc[2]
    id = acc[1].to_s
    path = "~/ayadn/#{File.basename(acc[3])}"
    if acc[4] == 1
      username = username.color(:green)
      id = id.color(:green)
      path = path.color(:green)
    end
    table << [username, id, path]
  end
  puts table
  puts "\n"
end

#switch(user) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ayadn/switch.rb', line 36

def switch(user)
  if user.empty? || user.nil?
    @status.no_username
    exit
  end
  username = Workers.new.remove_arobase_if_present([user.first])[0]
  accounts = Databases.all_accounts(@acc_db)
  please if accounts.empty?
  active = accounts.select { |acc| acc[4] == 1 }[0]
  active_user = active[0]
  if username == active_user
    @status.say do
      @thor.say_status :done, "already authorized with username @#{username}", :green
    end
    exit
  end
  flag = accounts.select { |acc| acc[0] == username }.flatten
  if flag.empty?
    @status.say do
      @thor.say_status :error, "@#{username} isn't in the database", :red
      @thor.say_status :next, "please run `ayadn -auth` to authorize this account", :yellow
    end
    exit
  else
    @status.say do
      @thor.say_status :switching, "from @#{active_user} to @#{username}", :cyan
      Databases.(@acc_db, username)
      @thor.say_status :done, "@#{username} is now the active account", :green
    end
    exit
  end
end