Class: NameSplitter::Splitter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fullname = "") ⇒ Splitter

Returns a new instance of Splitter.



12
13
14
15
16
17
18
19
# File 'lib/name_splitter.rb', line 12

def initialize(fullname = "")
  self.salutation = ""
  self.first_name = ""
  self.middle_name = ""
  self.last_name = ""
  self.suffix = ""
  self.name = fullname if fullname and !fullname.to_s.empty?
end

Instance Attribute Details

#first_nameObject

Returns the value of attribute first_name.



5
6
7
# File 'lib/name_splitter.rb', line 5

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



5
6
7
# File 'lib/name_splitter.rb', line 5

def last_name
  @last_name
end

#last_name_prefixObject

Returns the value of attribute last_name_prefix.



5
6
7
# File 'lib/name_splitter.rb', line 5

def last_name_prefix
  @last_name_prefix
end

#middle_nameObject

Returns the value of attribute middle_name.



5
6
7
# File 'lib/name_splitter.rb', line 5

def middle_name
  @middle_name
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/name_splitter.rb', line 6

def name
  @name
end

#salutationObject

Returns the value of attribute salutation.



5
6
7
# File 'lib/name_splitter.rb', line 5

def salutation
  @salutation
end

#suffixObject

Returns the value of attribute suffix.



5
6
7
# File 'lib/name_splitter.rb', line 5

def suffix
  @suffix
end

#suffixesObject

Returns the value of attribute suffixes.



5
6
7
# File 'lib/name_splitter.rb', line 5

def suffixes
  @suffixes
end

Class Method Details

.call(fullname) ⇒ Object



8
9
10
# File 'lib/name_splitter.rb', line 8

def self.call(fullname)
  new(fullname)
end

Instance Method Details

#anded_first_names?(_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/name_splitter.rb', line 72

def anded_first_names?(_name)
  ["and", "&"].include?(_name.to_s.strip)
end

#contains_last_name_prefix(name_arr) ⇒ Object



81
82
83
# File 'lib/name_splitter.rb', line 81

def contains_last_name_prefix(name_arr)
  last_name_prefix.collect { |x| x.upcase }.include?(name_arr.first.upcase)
end

#contains_middle_name(name_arr) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/name_splitter.rb', line 53

def contains_middle_name(name_arr)
  #checks whether the array of names passed in contains a likely middle name
  (name_arr.length == 2 and
    !(contains_suffix(name_arr) or contains_last_name_prefix(name_arr))) or
  (name_arr.length == 3 and
    !(contains_suffix(name_arr) and contains_last_name_prefix(name_arr))) or
  name_arr.length > 3
end

#contains_salutation(_name) ⇒ Object



76
77
78
79
# File 'lib/name_splitter.rb', line 76

def contains_salutation(_name)
  return false unless _name
  salutations.collect { |x| x.upcase }.include?(_name.gsub(/[.,;']+/, "").upcase)
end

#contains_suffix(name_arr) ⇒ Object



85
86
87
88
89
# File 'lib/name_splitter.rb', line 85

def contains_suffix(name_arr)
  raise "contains_suffix must receive an array" if !name_arr.class.name == "Array"
  return false if name_arr.length == 1
  suffixes.collect { |x| x.upcase }.include?(name_arr.last.gsub(/[.,;']+/, "").upcase)
end

#is_second_first_name?(_name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/name_splitter.rb', line 68

def is_second_first_name?(_name)
 second_first_names.collect { |x| x.upcase }.include?(_name.upcase)
end

#last_name_check(last_name_arr) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/name_splitter.rb', line 43

def last_name_check(last_name_arr)
  #accepts either a string or an array
  if last_name_arr.class.name == "String"
    last_name_arr = last_name_arr.split(" ")
  end
  return false if last_name_arr.empty?
  self.suffix = last_name_arr.pop if contains_suffix(last_name_arr)
  self.last_name = last_name_arr.join(" ").gsub(/[.,]+/, "")
end

#number_of_salutations(name_arr) ⇒ Object



62
63
64
65
66
# File 'lib/name_splitter.rb', line 62

def number_of_salutations(name_arr)
  return 3 if contains_salutation(name_arr[0]) & anded_first_names?(name_arr[1]) & contains_salutation(name_arr[2])
  return 1 if contains_salutation(name_arr[0])
  return 0
end

#salutationsObject



99
100
101
# File 'lib/name_splitter.rb', line 99

def salutations
  %w{Mr Mrs Ms Miss Dr Prof Rev Capt Sister Honorable Judge Chief}
end

#second_first_namesObject



103
104
105
# File 'lib/name_splitter.rb', line 103

def second_first_names
  %w{Beth Catherine Louise}
end