14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/person_name/name.rb', line 14
def full_name= new_name
return if full_name == new_name
parts = new_name.split " "
names = []
stage = :prefix
remember = nil
parts.each_with_index do |part, index|
is_upcase = part.first == part.first.upcase
has_dash = part.include? "-"
is_last = (parts.length - 1) == index
fp = [remember, part].compact.join(" ")
remember = nil
did_remember = (fp != part)
if valid_prefix?(part) and stage == :prefix names = add_to_last_if :prefix, fp, names
elsif valid_suffix?(part) and stage == :name names = add_to_last_if :suffix, fp, names
elsif part == "-" if last_stored = names.pop remember = [last_stored[0], fp].compact.join(" ")
else
remember = fp
end
elsif !is_upcase and !did_remember names = add_to_last_if :intercalation, fp, names
stage = :name
elsif !is_upcase and did_remember
remember = fp
elsif is_upcase and !has_dash
names << [fp, :name]
stage = :name
elsif is_upcase and has_dash
if fp.ends_with? "-"
if is_last
names << [fp, :name]
stage = :name
else
remember = fp
end
else
if fp.starts_with?("-") and last_stored = names.pop
fp = [last_stored[0], fp].compact.join(" ")
end
dash_parts = fp.split "-"
if dash_parts.last.first == dash_parts.last.first.upcase
names << [fp, :name]
stage = :name
elsif is_last
names << [fp, :name]
stage = :name
else
remember = fp
end
end
end
end
new_name = {}
stage = O[:prefix]
names.each_with_index do |value, index|
name, name_type = *value
stage = recheck_stage(name, stage)
if name_type == :prefix and stage == O[:prefix]
elsif name_type == :suffix and (index == names.length - 1)
stage = [O[:suffix], stage].max
elsif name_type == :suffix and (index != names.length - 1)
name_type = :name
end
if name_type == :name
if (index == names.length - 1) or new_name[:intercalation]
stage = [O[:last_name], stage].max
else
if !new_name[:first_name]
stage = [O[:first_name], stage].max
else
stage = [O[:middle_name], stage].max
end
end
elsif name_type == :intercalation
stage = [O[:intercalation], stage].max
end
new_name = add_part(new_name, stage, name)
end
NAME_PARTS.each do |part|
self.send "#{part}=", (new_name[part.to_sym] || "").blank? ? nil : new_name[part.to_sym]
end
end
|