class FullNameSplitter

Constants

HONORIFICS
PREFIXES

Public Class Methods

new(full_name, honorific=false) click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 6
def initialize(full_name, honorific=false)
  full_name ||= ''
  @full_name  = full_name.to_s.strip.gsub(/\s+/, ' ')
  @honorific = [] if honorific
  @first_name = []
  @last_name  = []
  split!
end

Public Instance Methods

first_name() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 54
def first_name
  @first_name.empty? ? nil : @first_name.join(' ')
end
honorific() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 50
def honorific
  @honorific.nil? || @honorific.empty? ? nil : @honorific[0].gsub(/[^\w]/, '')
end
last_name() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 58
def last_name
  @last_name.empty? ? nil : @last_name.join(' ')
end
split(name, honorific=false) click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 47
def split(name, honorific=false)
end
split!() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 15
def split!
  # Reset these
  @first_name = []
  @last_name  = []
  @honorific  = [] if honorific?

  # deals with comma, eg. Smith, John => John Smith
  tokens = @full_name.split(',')
  if tokens.size == 2
    @full_name = (tokens[1] + ' ' + tokens[0]).lstrip
  end

  @units = @full_name.split(/\s+/)
  while @unit = @units.shift do
    if honorific?
      @honorific << @unit
    elsif prefix? or with_apostrophe? or (first_name? and last_unit? and not initial?) or (has_honorific? and last_unit? and not first_name?)
      @last_name << @unit and break
    else
      @first_name << @unit
    end
  end
  @last_name += @units

  adjust_exceptions!
end
split_with_honorific(name) click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 43
def split_with_honorific(name)
  split(name, true)
end

Private Instance Methods

adjust_exceptions!() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 95
def adjust_exceptions!
  return if @first_name.size <= 1

  # Adjusting exceptions like
  # "Ludwig Mies van der Rohe"      => ["Ludwig",         "Mies van der Rohe"   ]
  # "Juan Martín de la Cruz Gómez"  => ["Juan Martín",    "de la Cruz Gómez"    ]
  # "Javier Reyes de la Barrera"    => ["Javier",         "Reyes de la Barrera" ]
  # Rosa María Pérez Martínez Vda. de la Cruz
  #                                 => ["Rosa María",     "Pérez Martínez Vda. de la Cruz"]
  if last_name =~ /^(van der|(vda\. )?de la \w+$)/i
    loop do
      @last_name.unshift @first_name.pop
      break if @first_name.size <= 2
    end
  end
end
first_name?() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 91
def first_name?
  not @first_name.empty?
end
has_honorific?() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 68
def has_honorific?
  not @honorific.nil? and not @honorific.empty?
end
honorific?() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 64
def honorific?
  !@honorific.nil? && HONORIFICS.include?(@unit.downcase.gsub(/[^\w]/, '')) && @honorific.empty? && @first_name.empty? && @last_name.empty?
end
initial?() click to toggle source

M or W.

# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 77
def initial?
  @unit =~ /^\w\.?$/
end
last_unit?() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 87
def last_unit?
  @units.empty?
end
prefix?() click to toggle source
# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 72
def prefix?
  PREFIXES.include?(@unit.downcase)
end
with_apostrophe?() click to toggle source

O'Connor, d'Artagnan match Noda' doesn't match

# File lib/generators/authkit/templates/lib/full_name_splitter.rb, line 83
def with_apostrophe?
  @unit =~ /\w{1}'\w+/
end