Class: Namie::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/namie/parser.rb

Overview

Name parser

Constant Summary collapse

TITLES =
/^(Mister|Mr\.?|Sr\.?|Sir|Senhor|Dr\.?|Doutor|Sra\.?|Senhora)\b/i
JURIDIC =
/\b(Ltd\.?|LTDA|Gmbh|LLC|S\/A)$/i
SUFFIXES =
/\b(#{JURIDIC}|Junior|Jr\.?|Neto|II|III)$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
15
16
# File 'lib/namie/parser.rb', line 9

def initialize(params)
  hsh, txt = params.partition { |pm| pm.respond_to?(:key) }
  hsh.each { |k, v| send("#{k}=", v) }
  @txt = txt.map { |t| t.split(' ') }.flatten
  remove_non_names
  parse_name
  normalize
end

Instance Attribute Details

#firstObject

Returns the value of attribute first.



7
8
9
# File 'lib/namie/parser.rb', line 7

def first
  @first
end

#lastObject

Returns the value of attribute last.



7
8
9
# File 'lib/namie/parser.rb', line 7

def last
  @last
end

#middleObject

Returns the value of attribute middle.



7
8
9
# File 'lib/namie/parser.rb', line 7

def middle
  @middle
end

#suffixObject

Returns the value of attribute suffix.



7
8
9
# File 'lib/namie/parser.rb', line 7

def suffix
  @suffix
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/namie/parser.rb', line 7

def title
  @title
end

#txtObject

Returns the value of attribute txt.



7
8
9
# File 'lib/namie/parser.rb', line 7

def txt
  @txt
end

Instance Method Details

#argsObject



35
36
37
# File 'lib/namie/parser.rb', line 35

def args
  [title, first, middle, last, suffix]
end

#normalizeObject



28
29
30
31
32
33
# File 'lib/namie/parser.rb', line 28

def normalize
  [:title, :middle, :suffix].each do |v|
    val =  send(v).reject(&:nil?).any? ? send(v).join(' ') : nil
    instance_variable_set("@#{v}", val)
  end
end

#parse_nameObject



18
19
20
21
# File 'lib/namie/parser.rb', line 18

def parse_name
  @txt.push(@txt.shift.tr(',', '')) if txt.first  =~ /,/
  @first, *@middle, @last = txt.size > 2 ? txt : txt.insert(1, nil)
end

#remove_non_namesObject



23
24
25
26
# File 'lib/namie/parser.rb', line 23

def remove_non_names
  @title, @txt = txt.partition { |a| a =~ TITLES }
  @suffix, @txt = txt.partition { |a| a =~ SUFFIXES }
end