Class: Namie::Parser
- Inherits:
-
Object
- Object
- Namie::Parser
- 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
-
#first ⇒ Object
Returns the value of attribute first.
-
#last ⇒ Object
Returns the value of attribute last.
-
#middle ⇒ Object
Returns the value of attribute middle.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#title ⇒ Object
Returns the value of attribute title.
-
#txt ⇒ Object
Returns the value of attribute txt.
Instance Method Summary collapse
- #args ⇒ Object
-
#initialize(params) ⇒ Parser
constructor
A new instance of Parser.
- #normalize ⇒ Object
- #parse_name ⇒ Object
- #remove_non_names ⇒ Object
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
#first ⇒ Object
Returns the value of attribute first.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def first @first end |
#last ⇒ Object
Returns the value of attribute last.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def last @last end |
#middle ⇒ Object
Returns the value of attribute middle.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def middle @middle end |
#suffix ⇒ Object
Returns the value of attribute suffix.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def suffix @suffix end |
#title ⇒ Object
Returns the value of attribute title.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def title @title end |
#txt ⇒ Object
Returns the value of attribute txt.
7 8 9 |
# File 'lib/namie/parser.rb', line 7 def txt @txt end |
Instance Method Details
#args ⇒ Object
35 36 37 |
# File 'lib/namie/parser.rb', line 35 def args [title, first, middle, last, suffix] end |
#normalize ⇒ Object
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_name ⇒ Object
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_names ⇒ Object
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 |