HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Usertool to Edit Forum Members from Desktop

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
editdesktopusertoolforummembersfrom

Problem

I've created a Forum website tool for admins to add, edit, and delete users from their desktop, saving to a file, and uploading the file to the database, the program is not completed I'm posting it here for review thus far

(People I'm creating it for would prefer me not having access to database, I understand I would be using activerecord model/structure if I where saving directly to the database.)

What the program does is simple:

  • It prompts the user for which he/she would like to do via add, edit,


delete users
.

  • The program then runs through a case statement of available options


and if the option isn't found it exits the program

  • In the add_user method the program saves the input of the user into a YAML file, and prompts the user for more users, if none follow exits the program.



  • During the delete_user method, the program does the same as adding except deletes the users from the YAML file there is a bug where the program deletes the entire YAML file, not just the edited users



  • In the edit_user the program will edit various elements of the hash inside the YAML file and save over the old element of the file. Not ready yet will post follow up later




The edit user section hasn't been setup yet, still working out the kinks in delete_user method.

So what I'm looking for is:

  • A possible explanation of why the delete_user method wipes the


entire file and not just the user entered, is this because I'm deleting
the entire section and not just the users input?

  • Would using a JSON file be better then using a YAML file, I'm aware


that A LOT of websites don't support YAML's so would it be a good idea to convert the require 'yaml' to a require 'json' and regroup from there?

  • Any possible areas of improvement? Meaning do you notice anything


right off the bat that should be changed, can my syntax be updated or more developed?

Source Code:

```
#!/usr/bin/env ruby

require 'yaml'
require 'open-uri'

def menu
print "Welcome to

Solution

prompt function

So many times you do:

puts "Please enter Username:" # Or something else instead of Username.
username = gets.chomp


In short you print and then read input, what about a function:

def prompt(message)
  puts message
  gets.chomp
end


You can reuse it and save a lot of repetition.

Functional Programming -- No mutation

Ruby leans towards the Function Programming paradigm, so you should stick to it.

One thing is avoiding mutation:

data = {username: username}
...
data[:email_address] = email
...
data[:member_status] = status


You modify data piece by piece, instead do:

...
...
data = {username: username, email_address: email, member_status: status}


It also looks more organized to me.

In fact you may write:

data = { username: prompt("The username? "),
         email_address: prompt("The email address? ")
         member_status: prompt("The member status? ") }


And make the code shorter and more readable.

Code Snippets

puts "Please enter Username:" # Or something else instead of Username.
username = gets.chomp
def prompt(message)
  puts message
  gets.chomp
end
data = {username: username}
...
data[:email_address] = email
...
data[:member_status] = status
...
...
data = {username: username, email_address: email, member_status: status}
data = { username: prompt("The username? "),
         email_address: prompt("The email address? ")
         member_status: prompt("The member status? ") }

Context

StackExchange Code Review Q#109874, answer score: 3

Revisions (0)

No revisions yet.