forked from sf-wdi-18/username_generator
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completed First Ruby Asg - Ming #7
Open
rollba14
wants to merge
2
commits into
SF-WDI-LABS:master
Choose a base branch
from
rollba14:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,93 @@ | |
# Run `rspec /spec/username_spec.rb` to get started. | ||
|
||
def format_name(first, last) | ||
nil | ||
user_name = "" | ||
if !first.empty? && !last.empty? | ||
user_name = first.strip[0].downcase + last.strip.split.join('') | ||
else | ||
return nil | ||
end | ||
user_name.downcase | ||
end | ||
|
||
def format_year | ||
nil | ||
def format_year(num) | ||
if (num.to_s.length-1) == 3 | ||
return num.to_s[2,3] | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
def build_username | ||
nil | ||
def check_privilege(num = 0) | ||
num = num.floor | ||
if num == 0 | ||
return 'user' | ||
elsif num == 1 | ||
return 'seller' | ||
elsif num == 2 | ||
return 'manager' | ||
elsif num >= 3 | ||
return 'admin' | ||
end | ||
end | ||
|
||
def user_type_prefix (num) | ||
if num == 0 | ||
return "" | ||
elsif num == 1 | ||
return 'seller-' | ||
elsif num == 2 | ||
return 'manager-' | ||
elsif num == 3 | ||
return 'admin-' | ||
end | ||
end | ||
|
||
|
||
def build_username(first, last, year, p_level = 0) | ||
user_type_prefix(p_level).to_s + format_name(first, last) + year.to_s[-2, year.to_s.length-1] | ||
end | ||
|
||
|
||
|
||
$users = [] | ||
|
||
def generate_username (first, last, year, p_level = 0) | ||
|
||
# put in variable for easy reference | ||
new_username = format_name(first,last) + year.to_s[-2, year.to_s.length-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have a method to format the year!
|
||
# First check if our list is empty, if so, we just generate a new user | ||
# with no incremental names attach, initialize the count, push into | ||
# the user list and return that first registered name, ELSE => | ||
# From a list of users, check if new_username match any of the currently | ||
# registered user name, if match, add '_count' to new_username, update count | ||
# and then push into increment_name list associated with that matched user | ||
# and then return that incremented name as output | ||
if $users[0] == nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great solution! |
||
user = { | ||
:name => new_username, | ||
:increment_names => [], | ||
:count => 1, | ||
} | ||
$users.push(user) | ||
return new_username | ||
else | ||
$users.each do |user| | ||
if user[:name] == (new_username) | ||
new_username = new_username + "_" + user[:count].to_s | ||
user[:count] = user[:count].to_i + 1 | ||
user[:increment_names].push(new_username) | ||
return new_username | ||
end | ||
end | ||
# if by any chance our list does not contain any duplicate names | ||
# we create a fresh hash, push it to the list and return that name | ||
user = { | ||
:name => new_username, | ||
:increment_names => [], | ||
:count => 1, | ||
} | ||
$users.push(user) | ||
return new_username | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
# Run `rspec spec/the_warmup_spec.rb` to get started. | ||
|
||
def say_hello | ||
"hi" | ||
"hello" | ||
end | ||
|
||
def scream(message) | ||
message | ||
message.upcase | ||
message = message.upcase + "!" | ||
return message | ||
end | ||
|
||
def first_char | ||
"z" | ||
def first_char(word) | ||
word[0].downcase | ||
end | ||
|
||
def polly_wanna | ||
"crackercrackercracker" | ||
def polly_wanna(word) | ||
word*3 | ||
end | ||
|
||
def after_you | ||
'no, after you' | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting some code smell - you've already written this code before. How can you refactor this code by calling the check_privilege method?