Class: User

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

Overview

An instance of User should be made for everyone who logs in or signs up

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, uid, playlist = [], mylist = []) ⇒ User

Returns a new instance of User.



8
9
10
11
12
13
14
15
# File 'lib/user.rb', line 8

def initialize(username, password, uid, playlist = [], mylist = [])
  @username = username
  @password = password
  @playlist = playlist
  @uid = uid
  @mylist = mylist
  @prompt = TTY::Prompt.new
end

Instance Attribute Details

#mylistObject

Returns the value of attribute mylist.



6
7
8
# File 'lib/user.rb', line 6

def mylist
  @mylist
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#playlistObject

Returns the value of attribute playlist.



6
7
8
# File 'lib/user.rb', line 6

def playlist
  @playlist
end

#uidObject (readonly)

Returns the value of attribute uid.



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

def uid
  @uid
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#change_password(new_password) ⇒ Object

Change password section Changes user name and routes back to menu



50
51
52
53
54
55
56
57
# File 'lib/user.rb', line 50

def change_password(new_password)
  update_password(new_password)
  @password = new_password
  puts 'Success! Your password has been changed.'.colorize(:light_green)
  @prompt.keypress('Press any key to continue..')
  menu = Menu.new(self)
  menu.
end

#change_username(new_name) ⇒ Object

Change Username section Changes user name and routes back to menu



29
30
31
32
33
34
35
36
# File 'lib/user.rb', line 29

def change_username(new_name)
  update_username(new_name)
  @username = new_name
  puts "Success! Your new username is #{@username}".colorize(:light_green)
  @prompt.keypress('Press any key to continue..')
  menu = Menu.new(self)
  menu.
end

#delete_accountObject

Delete account section Prompts user to confirm they would like to delete account.



71
72
73
74
75
76
77
78
79
80
# File 'lib/user.rb', line 71

def 
  puts 'Woah there! Deleting your account is serious business, and cannot be undone.'.colorize(:light_red)
  selection = @prompt.yes?('Are you sure you want to delete your account?'.colorize(:light_red))
  if selection
    delete_from_file
  else
    menu = Menu.new(.user)
    menu.
  end
end

#delete_from_fileObject

Deletes user data from the userfile



83
84
85
86
87
88
89
90
91
92
# File 'lib/user.rb', line 83

def delete_from_file
  updated_data = []
  .load_data.each { |user| updated_data << user unless user['id'] == .user.uid.to_s }
  File.open(.userdata, 'w') do |f|
    f.puts JSON.pretty_generate(updated_data)
  end
  puts 'Your account has been deleted. The program will now exit.'.colorize(:light_red)
  @prompt.keypress('Press any key to continue..')
  exit
end

#detailsObject

Prints user details



18
19
20
21
22
23
24
25
# File 'lib/user.rb', line 18

def details
  puts "Username: #{@username.colorize(:light_green)}"
  puts "Password: #{@password.colorize(:light_green)}"
  puts "User ID: #{@uid.to_s.colorize(:light_green)}"
  @prompt.keypress('Press any key to continue..')
  menu = Menu.new(self)
  menu.
end

#update_password(new_password) ⇒ Object

Updates user password in file



60
61
62
63
64
65
66
67
# File 'lib/user.rb', line 60

def update_password(new_password)
  updated_data = .load_data.each do |user|
    user['password'] = new_password if user['id'] == .user.uid.to_s
  end
  File.open(.userdata, 'w') do |f|
    f.puts JSON.pretty_generate(updated_data)
  end
end

#update_username(new_name) ⇒ Object

Updates username in file



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

def update_username(new_name)
  updated_data = .load_data.each do |user|
    user['username'] = new_name if user['id'] == @uid.to_s
  end
  File.open(.userdata, 'w') do |f|
    f.puts JSON.pretty_generate(updated_data)
  end
end