Class: FullNameSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/authkit/templates/lib/full_name_splitter.rb

Constant Summary collapse

PREFIXES =
%w(de da la du del dei vda. dello della degli delle van von der den heer ten ter vande vanden vander voor ver aan mc mac ben ibn bint al).freeze
HONORIFICS =
%w(mr mrs miss ms dr capt ofc rev prof sir cr hon).freeze

Instance Method Summary collapse

Constructor Details

#initialize(full_name, honorific = false) ⇒ FullNameSplitter

Returns a new instance of FullNameSplitter.



6
7
8
9
10
11
12
13
# 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

Instance Method Details

#first_nameObject



54
55
56
# File 'lib/generators/authkit/templates/lib/full_name_splitter.rb', line 54

def first_name
  @first_name.empty? ? nil : @first_name.join(' ')
end

#honorificObject



50
51
52
# 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_nameObject



58
59
60
# 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) ⇒ Object



47
48
# File 'lib/generators/authkit/templates/lib/full_name_splitter.rb', line 47

def split(name, honorific=false)
end

#split!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



43
44
45
# File 'lib/generators/authkit/templates/lib/full_name_splitter.rb', line 43

def split_with_honorific(name)
  split(name, true)
end