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_names?(_name) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/name_splitter.rb', line 84

def anded_names?(_name)
  contains_an_and(_name)
end

#contains_an_and(*name_arr) ⇒ Object



88
89
90
# File 'lib/name_splitter.rb', line 88

def contains_an_and(*name_arr)
  name_arr.flatten.select { |_name| ["and", "&"].include?(_name.to_s.strip) }.any?
end

#contains_last_name_prefix(name_arr) ⇒ Object



97
98
99
# File 'lib/name_splitter.rb', line 97

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



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

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



92
93
94
95
# File 'lib/name_splitter.rb', line 92

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

#contains_suffix(name_arr) ⇒ Object



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

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)


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

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

#last_name_check(last_name_arr) ⇒ Object



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

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_anded_names_before_last_names_if_any(name_arr) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/name_splitter.rb', line 71

def number_of_anded_names_before_last_names_if_any(name_arr)
  return 1 unless contains_an_and(name_arr)
  first_name_length = name_arr.length - 1
  first_name_length -= 1 if contains_suffix(name_arr)
  first_name_length -= 1 if contains_last_name_prefix(name_arr)
  first_name_length
end

#number_of_first_names(name_arr) ⇒ Object



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

def number_of_first_names(name_arr)
  return 2 if is_second_first_name?(name_arr[1])
  number_of_anded_names_before_last_names_if_any(name_arr)
end

#number_of_salutations(name_arr) ⇒ Object



60
61
62
63
64
# File 'lib/name_splitter.rb', line 60

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

#salutationsObject



115
116
117
# File 'lib/name_splitter.rb', line 115

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

#second_first_namesObject



119
120
121
# File 'lib/name_splitter.rb', line 119

def second_first_names
  %w{Beth Catherine Louise}
end