Skip to content

Commit

Permalink
Added splitting by comma
Browse files Browse the repository at this point in the history
  • Loading branch information
pahanix committed Nov 15, 2009
1 parent bf304cf commit 18b280f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ For other examples see _spec/lib/full\_name\_splitter\_spec.rb_
Also you can use splitter directly by calling module function FullNameSplitter::split

FullNameSplitter.split("Juan Martín de la Cruz Gómez") # => ["Juan Martín", "de la Cruz Gómez"]
FullNameSplitter.split("Ludwig Mies van der Rohe") # => ["Ludwig", "Mies van der Rohe"]
FullNameSplitter.split("Ludwig Mies van der Rohe") # => ["Ludwig", "Mies van der Rohe"]

If the lib can't split a name correctly, it is possible to split by comma

FullNameSplitter.split("John Quincy Adams") # => ["John Quincy", "Adams"]
FullNameSplitter.split("John, Quincy Adams") # => ["John", "Quincy Adams"]
8 changes: 6 additions & 2 deletions lib/full_name_splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ def full_name=(name)
private

def split(name)
splitter = Splitter.new(name)
[splitter.first_name, splitter.last_name]
if name.include?(',')
name.split(/\s*,\s*/, 2)
else
splitter = Splitter.new(name)
[splitter.first_name, splitter.last_name]
end
end

module_function :split
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/full_name_splitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def gum(first, last)
# Exceptions?
# the architect Ludwig Mies van der Rohe, from the West German city of Aachen, was originally Ludwig Mies;
"Ludwig Mies van der Rohe" => ["Ludwig", "Mies van der Rohe" ],

# If comma is provided then split by comma

"John, Quincy Adams" => ["John", "Quincy Adams" ],
"Ludwig Mies, van der Rohe" => ["Ludwig Mies", "van der Rohe" ],

}.

each do |full_name, split_name|
Expand Down

0 comments on commit 18b280f

Please sign in to comment.