Class: FullNameSplitter::Splitter

Inherits:
Object
  • Object
show all
Defined in:
lib/full-name-splitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(full_name) ⇒ Splitter

Returns a new instance of Splitter.



9
10
11
12
13
14
# File 'lib/full-name-splitter.rb', line 9

def initialize(full_name)
  @full_name  = full_name
  @first_name = []
  @last_name  = []
  split!
end

Instance Method Details

#first_nameObject



30
31
32
# File 'lib/full-name-splitter.rb', line 30

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

#last_nameObject



34
35
36
# File 'lib/full-name-splitter.rb', line 34

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

#split!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/full-name-splitter.rb', line 16

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

  adjust_exceptions!
end