A while ago, I posted about creating a user tool for forum admins that allows them to add, edit, and delete users from their desktops. This still is not complete, posting for review on tool so far
I've finished with the delete_user
method and also added a couple new features to it, what this code does, is take your input, and save it to a YAML
file ( will be converting YAML to JSON ). It's advantages are that you don't have to go directly to your database until you actually need to uplaod the file, so you can create an entire list of users from your desktop without sending one validation e-mail, or having to validate one user.
#!/usr/bin/env ruby
require 'yaml'
def menu
print "Welcome to User Menu. Would you like to [A]dd users, [D]elete users, [E]dit users, [V]iew all users, or [Q]uit the system?\n"
input = gets.chomp!
case input
when /a/i
add_user
when /d/i
delete_user
when /e/i
edit_user
when /v/i
view_all
else
puts "Exiting system.."
exit
end
end
def load_hash
File.exist?('users.yml') ? YAML.load_file('users.yml') : {users: {}}
end
def save_hash(hash)
File.open('users.yml', 'w') { |s| s.write(hash.to_yaml) }
end
def add_user
hash = load_hash
hash[:users][:"#{prompt('Enter username:')}"] =
{email_address: prompt("Enter email:"),
member_status: prompt("Enter member status:")}
save_hash(hash)
add_user if restart
end
def delete_user
hash = load_hash
to_delete = prompt("Enter username:").to_sym
if hash[:users].has_key?(to_delete)
hash[:users].delete(to_delete)
else
puts 'User not found'
end
save_hash(hash)
delete_user if restart
end
def edit_user
edit_user if restart
end
def view_all
puts load_hash
end
def restart
puts "Would you like to edit another user?"
input = gets.chomp
if input =~ /yes/i
return true
else
puts "Exiting program.."
exit
end
end
def prompt ( message )
puts message
gets.chomp
end
menu
Overview of the YAML
file:
--- :username: TEST :email_address: TEST :member_status: TEST
Is there anything here that I can do better? Better syntax, combine methods, edit something? I'm looking for every possible example.