person-name 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +2 -2
- data/Gemfile.lock +38 -38
- data/VERSION +1 -1
- data/lib/person_name/has_person_name.rb +34 -9
- data/lib/person_name/migration_support.rb +1 -0
- data/lib/person_name/name_splitter.rb +76 -42
- data/spec/database.yml +1 -1
- data/spec/person_name/has_person_name_spec.rb +23 -2
- metadata +4 -4
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -2,12 +2,12 @@ GEM
|
|
|
2
2
|
remote: http://rubygems.org/
|
|
3
3
|
specs:
|
|
4
4
|
abstract (1.0.0)
|
|
5
|
-
actionmailer (3.0.
|
|
6
|
-
actionpack (= 3.0.
|
|
5
|
+
actionmailer (3.0.1)
|
|
6
|
+
actionpack (= 3.0.1)
|
|
7
7
|
mail (~> 2.2.5)
|
|
8
|
-
actionpack (3.0.
|
|
9
|
-
activemodel (= 3.0.
|
|
10
|
-
activesupport (= 3.0.
|
|
8
|
+
actionpack (3.0.1)
|
|
9
|
+
activemodel (= 3.0.1)
|
|
10
|
+
activesupport (= 3.0.1)
|
|
11
11
|
builder (~> 2.1.2)
|
|
12
12
|
erubis (~> 2.6.6)
|
|
13
13
|
i18n (~> 0.4.1)
|
|
@@ -15,19 +15,19 @@ GEM
|
|
|
15
15
|
rack-mount (~> 0.6.12)
|
|
16
16
|
rack-test (~> 0.5.4)
|
|
17
17
|
tzinfo (~> 0.3.23)
|
|
18
|
-
activemodel (3.0.
|
|
19
|
-
activesupport (= 3.0.
|
|
18
|
+
activemodel (3.0.1)
|
|
19
|
+
activesupport (= 3.0.1)
|
|
20
20
|
builder (~> 2.1.2)
|
|
21
21
|
i18n (~> 0.4.1)
|
|
22
|
-
activerecord (3.0.
|
|
23
|
-
activemodel (= 3.0.
|
|
24
|
-
activesupport (= 3.0.
|
|
22
|
+
activerecord (3.0.1)
|
|
23
|
+
activemodel (= 3.0.1)
|
|
24
|
+
activesupport (= 3.0.1)
|
|
25
25
|
arel (~> 1.0.0)
|
|
26
26
|
tzinfo (~> 0.3.23)
|
|
27
|
-
activeresource (3.0.
|
|
28
|
-
activemodel (= 3.0.
|
|
29
|
-
activesupport (= 3.0.
|
|
30
|
-
activesupport (3.0.
|
|
27
|
+
activeresource (3.0.1)
|
|
28
|
+
activemodel (= 3.0.1)
|
|
29
|
+
activesupport (= 3.0.1)
|
|
30
|
+
activesupport (3.0.1)
|
|
31
31
|
arel (1.0.1)
|
|
32
32
|
activesupport (~> 3.0.0)
|
|
33
33
|
builder (2.1.2)
|
|
@@ -42,7 +42,7 @@ GEM
|
|
|
42
42
|
git (>= 1.2.5)
|
|
43
43
|
rubyforge (>= 2.0.0)
|
|
44
44
|
json_pure (1.4.6)
|
|
45
|
-
mail (2.2.
|
|
45
|
+
mail (2.2.7)
|
|
46
46
|
activesupport (>= 2.3.6)
|
|
47
47
|
mime-types
|
|
48
48
|
treetop (>= 1.4.5)
|
|
@@ -53,37 +53,37 @@ GEM
|
|
|
53
53
|
rack (1.2.1)
|
|
54
54
|
rack-mount (0.6.13)
|
|
55
55
|
rack (>= 1.0.0)
|
|
56
|
-
rack-test (0.5.
|
|
56
|
+
rack-test (0.5.6)
|
|
57
57
|
rack (>= 1.0)
|
|
58
|
-
rails (3.0.
|
|
59
|
-
actionmailer (= 3.0.
|
|
60
|
-
actionpack (= 3.0.
|
|
61
|
-
activerecord (= 3.0.
|
|
62
|
-
activeresource (= 3.0.
|
|
63
|
-
activesupport (= 3.0.
|
|
58
|
+
rails (3.0.1)
|
|
59
|
+
actionmailer (= 3.0.1)
|
|
60
|
+
actionpack (= 3.0.1)
|
|
61
|
+
activerecord (= 3.0.1)
|
|
62
|
+
activeresource (= 3.0.1)
|
|
63
|
+
activesupport (= 3.0.1)
|
|
64
64
|
bundler (~> 1.0.0)
|
|
65
|
-
railties (= 3.0.
|
|
66
|
-
railties (3.0.
|
|
67
|
-
actionpack (= 3.0.
|
|
68
|
-
activesupport (= 3.0.
|
|
65
|
+
railties (= 3.0.1)
|
|
66
|
+
railties (3.0.1)
|
|
67
|
+
actionpack (= 3.0.1)
|
|
68
|
+
activesupport (= 3.0.1)
|
|
69
69
|
rake (>= 0.8.4)
|
|
70
70
|
thor (~> 0.14.0)
|
|
71
71
|
rake (0.8.7)
|
|
72
72
|
rcov (0.9.9)
|
|
73
|
-
rspec (2.0.
|
|
74
|
-
rspec-core (
|
|
75
|
-
rspec-expectations (
|
|
76
|
-
rspec-mocks (
|
|
77
|
-
rspec-core (2.0.
|
|
78
|
-
rspec-expectations (2.0.
|
|
73
|
+
rspec (2.0.1)
|
|
74
|
+
rspec-core (~> 2.0.1)
|
|
75
|
+
rspec-expectations (~> 2.0.1)
|
|
76
|
+
rspec-mocks (~> 2.0.1)
|
|
77
|
+
rspec-core (2.0.1)
|
|
78
|
+
rspec-expectations (2.0.1)
|
|
79
79
|
diff-lcs (>= 1.1.2)
|
|
80
|
-
rspec-mocks (2.0.
|
|
81
|
-
rspec-core (
|
|
82
|
-
rspec-expectations (
|
|
80
|
+
rspec-mocks (2.0.1)
|
|
81
|
+
rspec-core (~> 2.0.1)
|
|
82
|
+
rspec-expectations (~> 2.0.1)
|
|
83
83
|
rubyforge (2.0.4)
|
|
84
84
|
json_pure (>= 1.1.7)
|
|
85
85
|
sqlite3-ruby (1.3.1)
|
|
86
|
-
thor (0.14.
|
|
86
|
+
thor (0.14.3)
|
|
87
87
|
treetop (1.4.8)
|
|
88
88
|
polyglot (>= 0.3.1)
|
|
89
89
|
tzinfo (0.3.23)
|
|
@@ -95,7 +95,7 @@ DEPENDENCIES
|
|
|
95
95
|
jeweler
|
|
96
96
|
mysql
|
|
97
97
|
pg
|
|
98
|
-
rails (= 3.0.
|
|
98
|
+
rails (= 3.0.1)
|
|
99
99
|
rcov
|
|
100
|
-
rspec (= 2.0.
|
|
100
|
+
rspec (= 2.0.1)
|
|
101
101
|
sqlite3-ruby
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.5
|
|
@@ -40,22 +40,46 @@ module PersonName
|
|
|
40
40
|
base.send :include, PersonName::ActiveRecord::Core::InstanceMethods
|
|
41
41
|
base.extend PersonName::ActiveRecord::Core::ClassMethods
|
|
42
42
|
base.initialize_person_names
|
|
43
|
+
#base.define_scopes
|
|
43
44
|
end
|
|
44
45
|
|
|
45
46
|
module ClassMethods
|
|
47
|
+
|
|
46
48
|
def initialize_person_names
|
|
47
49
|
name_types.map(&:to_s).each do |name_type|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
script = %(
|
|
51
|
+
def #{name_type}
|
|
52
|
+
person_name_for('#{name_type}')
|
|
53
|
+
end
|
|
54
|
+
)
|
|
55
|
+
script += %(
|
|
56
|
+
def #{name_type}= new_name
|
|
57
|
+
set_person_name_for('#{name_type}', new_name)
|
|
58
|
+
end
|
|
59
|
+
)
|
|
60
|
+
class_eval script
|
|
57
61
|
end
|
|
58
62
|
end
|
|
63
|
+
|
|
64
|
+
# def define_scopes
|
|
65
|
+
# name_types.map(&:to_s).each do |field_name|
|
|
66
|
+
# concat_construct = [%(IFNULL(`#{field_name}_prefix`, ""))]
|
|
67
|
+
# [:first_name, :middle_name, :intercalation, :last_name, :suffix].each { |element|
|
|
68
|
+
# concat_construct << %(IF(ISNULL(`#{field_name}_#{element}`), "", CONCAT(" ", `#{field_name}_#{element}`)))
|
|
69
|
+
# }
|
|
70
|
+
# script = %(
|
|
71
|
+
# scope :with_#{field_name}, lambda { |name|
|
|
72
|
+
# where('TRIM(CONCAT(#{concat_construct.join(", ")})) = ?', name)
|
|
73
|
+
# }
|
|
74
|
+
# def find_by_#{field_name}(name)
|
|
75
|
+
# self.with_#{field_name}(name).first
|
|
76
|
+
# end
|
|
77
|
+
# )
|
|
78
|
+
# puts script
|
|
79
|
+
# instance_eval script
|
|
80
|
+
# end
|
|
81
|
+
# end
|
|
82
|
+
|
|
59
83
|
end
|
|
60
84
|
|
|
61
85
|
module InstanceMethods
|
|
@@ -66,6 +90,7 @@ module PersonName
|
|
|
66
90
|
end
|
|
67
91
|
|
|
68
92
|
def set_person_name_for field, new_name
|
|
93
|
+
write_attribute(field, new_name)
|
|
69
94
|
person_name_for(field).full_name = new_name
|
|
70
95
|
end
|
|
71
96
|
|
|
@@ -3,67 +3,101 @@ module PersonName
|
|
|
3
3
|
|
|
4
4
|
NAME_PARTS = %w(prefix first_name middle_name intercalation last_name suffix)
|
|
5
5
|
|
|
6
|
+
DEFAULT_PREFIXES = %w(Mr. Dhr. Drs. Prof. Mevr. Mej. Mrs.)
|
|
7
|
+
DEFAULT_SUFFIXES = %w(jr. Phd.)
|
|
8
|
+
|
|
9
|
+
# Splits the given name in individual name parts in form of a hash.
|
|
10
|
+
# You can supply a hash with existing values for editing purposes.
|
|
6
11
|
def self.split(new_name_str, existing_values = {})
|
|
7
12
|
parts = new_name_str.split " "
|
|
8
13
|
names = []
|
|
9
14
|
stage = :prefix
|
|
15
|
+
|
|
16
|
+
# value to remember for merging
|
|
10
17
|
remember = nil
|
|
11
18
|
parts.each_with_index do |part, index|
|
|
19
|
+
|
|
20
|
+
# some helper variables
|
|
12
21
|
is_upcase = (part[0,1] == part[0,1].upcase)
|
|
13
22
|
has_dash = part.include? "-"
|
|
14
23
|
is_last = (parts.length - 1) == index
|
|
15
24
|
|
|
16
|
-
|
|
25
|
+
# add the remember part to the current part but make a note if we used a remembered value
|
|
26
|
+
full_part = [remember, part].compact.join(" ")
|
|
27
|
+
did_remember = (full_part != part)
|
|
28
|
+
suffix_remember = remember
|
|
17
29
|
remember = nil
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
remember = fp
|
|
36
|
-
elsif is_upcase and !has_dash
|
|
37
|
-
names << [fp, :name]
|
|
38
|
-
stage = :name
|
|
39
|
-
elsif is_upcase and has_dash
|
|
40
|
-
if fp.ends_with? "-"
|
|
41
|
-
if is_last
|
|
42
|
-
names << [fp, :name]
|
|
43
|
-
stage = :name
|
|
30
|
+
|
|
31
|
+
# Mr. Mrs. Mevr. Mej.
|
|
32
|
+
if valid_prefix?(part) and stage == :prefix and not did_remember # if the part is a valid prefix, mark it as such
|
|
33
|
+
names = add_to_last_if :prefix, part, names
|
|
34
|
+
|
|
35
|
+
# Jr. Phd.
|
|
36
|
+
elsif valid_suffix?(part) # if the part is a valid suffix, mark it as such
|
|
37
|
+
names << [suffix_remember, :name] if did_remember
|
|
38
|
+
names = add_to_last_if :suffix, part, names
|
|
39
|
+
else
|
|
40
|
+
remember = nil
|
|
41
|
+
|
|
42
|
+
# Damen - van Valkenburg
|
|
43
|
+
if part == "-" # if the part is a dash
|
|
44
|
+
if last_stored = names.pop # retrieve the previous name part (if any) and store it with the dash
|
|
45
|
+
# for the part to come (remember)
|
|
46
|
+
remember = [last_stored[0], full_part].compact.join(" ")
|
|
44
47
|
else
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
else
|
|
48
|
-
if fp.starts_with?("-") and last_stored = names.pop
|
|
49
|
-
fp = [last_stored[0], fp].compact.join(" ")
|
|
48
|
+
# if there is no previous part, just store the current part for later
|
|
49
|
+
remember = full_part
|
|
50
50
|
end
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
|
|
52
|
+
# van de Sloot
|
|
53
|
+
elsif !is_upcase and !did_remember # intercalation words are never with a capital
|
|
54
|
+
names = add_to_last_if :intercalation, full_part, names
|
|
55
|
+
stage = :name
|
|
56
|
+
|
|
57
|
+
# Groen in het Woud
|
|
58
|
+
elsif !is_upcase and did_remember
|
|
59
|
+
remember = full_part
|
|
60
|
+
|
|
61
|
+
# Groen
|
|
62
|
+
elsif is_upcase and !has_dash
|
|
63
|
+
names << [full_part, :name]
|
|
64
|
+
stage = :name
|
|
65
|
+
|
|
66
|
+
# Groen-teboer
|
|
67
|
+
elsif is_upcase and has_dash
|
|
68
|
+
if full_part.ends_with? "-"
|
|
69
|
+
if is_last
|
|
70
|
+
names << [full_part, :name]
|
|
71
|
+
stage = :name
|
|
72
|
+
else
|
|
73
|
+
remember = full_part
|
|
74
|
+
end
|
|
58
75
|
else
|
|
59
|
-
|
|
76
|
+
if full_part.starts_with?("-") and last_stored = names.pop
|
|
77
|
+
last_stored_name = last_stored[0].split(" ")
|
|
78
|
+
last_part = last_stored_name.pop
|
|
79
|
+
names << [last_stored_name.join(" "), last_stored[1]] unless last_stored_name.empty?
|
|
80
|
+
full_part = [last_part, full_part].compact.join(" ")
|
|
81
|
+
end
|
|
82
|
+
dash_parts = full_part.split "-"
|
|
83
|
+
if dash_parts.last[0,1] == dash_parts.last[0,1].upcase
|
|
84
|
+
names << [full_part, :name]
|
|
85
|
+
stage = :name
|
|
86
|
+
elsif is_last
|
|
87
|
+
names << [full_part, :name]
|
|
88
|
+
stage = :name
|
|
89
|
+
else
|
|
90
|
+
remember = full_part
|
|
91
|
+
end
|
|
60
92
|
end
|
|
61
93
|
end
|
|
62
94
|
end
|
|
63
95
|
end
|
|
96
|
+
names << [remember, :name] if remember
|
|
64
97
|
|
|
65
98
|
new_name = {}
|
|
66
99
|
stage = O[:prefix]
|
|
100
|
+
#puts names.inspect
|
|
67
101
|
|
|
68
102
|
names.each_with_index do |value, index|
|
|
69
103
|
name, name_type = *value
|
|
@@ -139,11 +173,11 @@ module PersonName
|
|
|
139
173
|
end
|
|
140
174
|
|
|
141
175
|
def self.valid_prefix? name_part
|
|
142
|
-
|
|
176
|
+
DEFAULT_PREFIXES.include? name_part
|
|
143
177
|
end
|
|
144
178
|
|
|
145
179
|
def self.valid_suffix? name_part
|
|
146
|
-
|
|
180
|
+
DEFAULT_SUFFIXES.include? name_part
|
|
147
181
|
end
|
|
148
182
|
|
|
149
183
|
end
|
data/spec/database.yml
CHANGED
|
@@ -61,11 +61,18 @@ describe "Has Person Name" do
|
|
|
61
61
|
test_cases = [
|
|
62
62
|
[nil, "Matthijs", nil, nil, "Groen", nil],
|
|
63
63
|
[nil, "Matthijs", "Jacobus", nil, "Groen", nil],
|
|
64
|
-
[nil, "Frans", nil, "van der", "Sluis",
|
|
64
|
+
[nil, "Frans", nil, "van der", "Sluis", "Phd."],
|
|
65
65
|
[nil, "Maria", "Cornelia Hendrina", nil, "Damen-van Valenberg", nil],
|
|
66
|
-
[
|
|
66
|
+
["Mevr.", "Maria", "Cornelia Hendrina", nil, "Damen - van Valenberg", nil],
|
|
67
67
|
[nil, "Maria", "Cornelia Hendrina", nil, "Damen- van Valenberg", nil],
|
|
68
68
|
[nil, "Maria", "Cornelia Hendrina", nil, "Damen -van Valenberg", nil],
|
|
69
|
+
[nil, "Maria", "Cornelia Hendrina", "van", nil, nil],
|
|
70
|
+
[nil, "- Maria", "Cornelia Hendrina", "van", "Dingen-", nil],
|
|
71
|
+
["Mevr.", "Maria", "Mej. Cornelia Hendrina", "van", "Dingen -", nil],
|
|
72
|
+
[nil, "Maria", "Cornelia Hendrina", "van", "Dingen-van", nil],
|
|
73
|
+
[nil, "Maria", "Cornelia Hendrina", "van", "Groen-Teboer", nil],
|
|
74
|
+
[nil, "Maria", "Cornelia Hendrina", "van", "Groen-van Phd. Lala", "jr."],
|
|
75
|
+
[nil, "Maria", "Cornelia Hendrina", "van", "Groen-van", "Phd. jr."],
|
|
69
76
|
[nil, "Dirk", "Jan", "van de", "Abeele", nil],
|
|
70
77
|
[nil, "Yolanthe", "Cabau", "van", "Kasbergen", nil],
|
|
71
78
|
]
|
|
@@ -75,6 +82,7 @@ describe "Has Person Name" do
|
|
|
75
82
|
@person = Person.new
|
|
76
83
|
@person.name = string
|
|
77
84
|
|
|
85
|
+
#puts "in #{fields.inspect}"
|
|
78
86
|
test_fields.each_with_index do |field, index|
|
|
79
87
|
@person.name.send(field).should == fields[index]
|
|
80
88
|
end
|
|
@@ -92,4 +100,17 @@ describe "Has Person Name" do
|
|
|
92
100
|
|
|
93
101
|
end
|
|
94
102
|
|
|
103
|
+
describe "dynamic scopes" do
|
|
104
|
+
before(:each) do
|
|
105
|
+
clean_database!
|
|
106
|
+
@person = Person.new :name => "Matthijs Jacobus Groen"
|
|
107
|
+
@person.save
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it "should have a working with_name scope" do
|
|
111
|
+
Person.find_by_name("Matthijs Jacobus Groen").should == @person
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
end
|
|
115
|
+
|
|
95
116
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: person-name
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 29
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 2
|
|
9
|
-
-
|
|
10
|
-
version: 0.2.
|
|
9
|
+
- 5
|
|
10
|
+
version: 0.2.5
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Matthijs Groen
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2010-
|
|
18
|
+
date: 2010-10-25 00:00:00 +02:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies: []
|
|
21
21
|
|