Class: User
- Inherits:
-
Object
- Object
- User
- 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
-
#mylist ⇒ Object
Returns the value of attribute mylist.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#playlist ⇒ Object
Returns the value of attribute playlist.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#change_password(new_password) ⇒ Object
Change password section Changes user name and routes back to menu.
-
#change_username(new_name) ⇒ Object
Change Username section Changes user name and routes back to menu.
-
#delete_account ⇒ Object
Delete account section Prompts user to confirm they would like to delete account.
-
#delete_from_file ⇒ Object
Deletes user data from the userfile.
-
#details ⇒ Object
Prints user details.
-
#initialize(username, password, uid, playlist = [], mylist = []) ⇒ User
constructor
A new instance of User.
-
#update_password(new_password) ⇒ Object
Updates user password in file.
-
#update_username(new_name) ⇒ Object
Updates username in file.
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
#mylist ⇒ Object
Returns the value of attribute mylist.
6 7 8 |
# File 'lib/user.rb', line 6 def mylist @mylist end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
5 6 7 |
# File 'lib/user.rb', line 5 def password @password end |
#playlist ⇒ Object
Returns the value of attribute playlist.
6 7 8 |
# File 'lib/user.rb', line 6 def playlist @playlist end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
5 6 7 |
# File 'lib/user.rb', line 5 def uid @uid end |
#username ⇒ Object (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.new(self) .account_details 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.new(self) .account_details end |
#delete_account ⇒ Object
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 delete_account 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.new(Login.user) .account_details end end |
#delete_from_file ⇒ Object
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 = [] Login.load_data.each { |user| updated_data << user unless user['id'] == Login.user.uid.to_s } File.open(Login.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 |
#details ⇒ Object
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.new(self) .account_details 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 = Login.load_data.each do |user| user['password'] = new_password if user['id'] == Login.user.uid.to_s end File.open(Login.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 = Login.load_data.each do |user| user['username'] = new_name if user['id'] == @uid.to_s end File.open(Login.userdata, 'w') do |f| f.puts JSON.pretty_generate(updated_data) end end |