-
-
Save notahat/39132 to your computer and use it in GitHub Desktop.
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
value = "Don T Alias" | |
first_name, last_name = value.reverse.split(' ', 2).reverse.collect(&:reverse) | |
first_name = first_name.to_s | |
last_name = last_name.to_s | |
# Refactored to take into account Xavier's pathological aversion to case statements: | |
words = value.split(' ') | |
first_name, last_name = if words.size == 0 | |
['', ''] | |
elsif words.size == 1 | |
[words.first, ''] | |
else | |
[words[0..-2].join(' '), words.last] | |
end | |
--- | |
it 'splits the value and assigns it to first name and last name' do | |
object.name = "Don Alias" | |
object.first_name.should == "Don" | |
object.last_name.should == 'Alias' | |
end | |
it 'splits the value and assigns every thing except the last word to first name, and last word to last name' do | |
object.name = "Don T Alias" | |
object.first_name.should == "Don T" | |
object.last_name.should == 'Alias' | |
end | |
it 'when only one name is provided it blanks out last name' do | |
object.name = "Don" | |
object.first_name.should == "Don" | |
object.last_name.should == '' | |
end | |
it 'when a blank string is provided it blanks both first and last name' do | |
object.name = "" | |
object.first_name.should == "" | |
object.last_name.should == '' | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment