Class: PersonName::Name

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

Constant Summary collapse

NAME_PARTS =
%w(prefix first_name middle_name intercalation last_name suffix)

Instance Method Summary collapse

Constructor Details

#initialize(naming_prefix, record) ⇒ Name

Returns a new instance of Name.



5
6
7
8
# File 'lib/person_name/name.rb', line 5

def initialize(naming_prefix, record)
	@naming_prefix = naming_prefix
	@record = record
end

Instance Method Details

#full_last_nameObject



118
119
120
# File 'lib/person_name/name.rb', line 118

def full_last_name
	[:intercalation, :last_name, :suffix].collect { |p| self.send(p) }.compact.join " "
end

#full_nameObject



10
11
12
# File 'lib/person_name/name.rb', line 10

def full_name
	NAME_PARTS.collect { |p| self.send(p) }.compact.join " "
end

#full_name=(new_name) ⇒ Object



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
	# Matthijs Jacobus Groen
	# Patricia Jose Huisman
	# Thijs van de Biggelaar
	# Michael Thijssens
	# Maria Cornelia Hendrina Damen-van Valenberg
	# Dirk Jan van den Abeele
	# Yolanthe Cabau van Kasbergen
	# Yolanthe Sneijder-Cabau
	return if full_name == new_name # no changes

	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 # if the part is a valid prefix, mark it as such
			names = add_to_last_if :prefix, fp, names
		elsif valid_suffix?(part) and stage == :name # if the part is a valid suffix, mark it as such
			names = add_to_last_if :suffix, fp, names
		elsif part == "-" # if the part is a dash
			if last_stored = names.pop # retrieve the previous name part (if any) and store it with the dash
				# for the part to come (remember)
				remember = [last_stored[0], fp].compact.join(" ")
			else
				# if there is no previous part, just store the current part for later
				remember = fp
			end
		elsif !is_upcase and !did_remember # intercalation words are never with a capital
			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
	# assignments
	NAME_PARTS.each do |part|
		self.send "#{part}=", (new_name[part.to_sym] || "").blank? ? nil : new_name[part.to_sym]
	end
end

#short_name(include_middle_names = true) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/person_name/name.rb', line 122

def short_name include_middle_names = true
	parts = []
	parts << "#{self.first_name.first.upcase}." if self.first_name
	if include_middle_names and self.middle_name
		names = self.middle_name.split(" ")
		names.each do |name|
			parts << "#{name.first.upcase}."
		end
	end
	[parts.join, full_last_name] * " "
end

#to_sObject



145
146
147
# File 'lib/person_name/name.rb', line 145

def to_s
	full_name
end