person-name 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  source :gemcutter
2
2
 
3
3
  # Rails 3.0
4
- gem 'rails', '3.0.0'
5
- gem 'rspec', '2.0.0.beta.22'
4
+ gem 'rails', '3.0.1'
5
+ gem 'rspec', '2.0.1'
6
6
  gem 'sqlite3-ruby', :require => 'sqlite3'
7
7
  gem 'mysql'
8
8
  gem 'pg'
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.0)
6
- actionpack (= 3.0.0)
5
+ actionmailer (3.0.1)
6
+ actionpack (= 3.0.1)
7
7
  mail (~> 2.2.5)
8
- actionpack (3.0.0)
9
- activemodel (= 3.0.0)
10
- activesupport (= 3.0.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.0)
19
- activesupport (= 3.0.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.0)
23
- activemodel (= 3.0.0)
24
- activesupport (= 3.0.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.0)
28
- activemodel (= 3.0.0)
29
- activesupport (= 3.0.0)
30
- activesupport (3.0.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.6.1)
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.5)
56
+ rack-test (0.5.6)
57
57
  rack (>= 1.0)
58
- rails (3.0.0)
59
- actionmailer (= 3.0.0)
60
- actionpack (= 3.0.0)
61
- activerecord (= 3.0.0)
62
- activeresource (= 3.0.0)
63
- activesupport (= 3.0.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.0)
66
- railties (3.0.0)
67
- actionpack (= 3.0.0)
68
- activesupport (= 3.0.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.0.beta.22)
74
- rspec-core (= 2.0.0.beta.22)
75
- rspec-expectations (= 2.0.0.beta.22)
76
- rspec-mocks (= 2.0.0.beta.22)
77
- rspec-core (2.0.0.beta.22)
78
- rspec-expectations (2.0.0.beta.22)
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.0.beta.22)
81
- rspec-core (= 2.0.0.beta.22)
82
- rspec-expectations (= 2.0.0.beta.22)
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.1)
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.0)
98
+ rails (= 3.0.1)
99
99
  rcov
100
- rspec (= 2.0.0.beta.22)
100
+ rspec (= 2.0.1)
101
101
  sqlite3-ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
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
- class_eval %(
49
- def #{name_type}
50
- person_name_for('#{name_type}')
51
- end
52
-
53
- def #{name_type}= new_name
54
- set_person_name_for('#{name_type}', new_name)
55
- end
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
 
@@ -10,6 +10,7 @@ module ActiveRecord
10
10
  name_parts.each do |part|
11
11
  column("#{name}_#{part}".to_sym, :string, :null => true)
12
12
  end
13
+ column("#{name}".to_sym, :string, :null => true)
13
14
  end
14
15
 
15
16
  end
@@ -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
- fp = [remember, part].compact.join(" ")
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
- did_remember = (fp != part)
19
- if valid_prefix?(part) and stage == :prefix # if the part is a valid prefix, mark it as such
20
- names = add_to_last_if :prefix, fp, names
21
- elsif valid_suffix?(part) and stage == :name # if the part is a valid suffix, mark it as such
22
- names = add_to_last_if :suffix, fp, names
23
- elsif part == "-" # if the part is a dash
24
- if last_stored = names.pop # retrieve the previous name part (if any) and store it with the dash
25
- # for the part to come (remember)
26
- remember = [last_stored[0], fp].compact.join(" ")
27
- else
28
- # if there is no previous part, just store the current part for later
29
- remember = fp
30
- end
31
- elsif !is_upcase and !did_remember # intercalation words are never with a capital
32
- names = add_to_last_if :intercalation, fp, names
33
- stage = :name
34
- elsif !is_upcase and did_remember
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
- remember = fp
46
- end
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
- dash_parts = fp.split "-"
52
- if dash_parts.last.first == dash_parts.last.first.upcase
53
- names << [fp, :name]
54
- stage = :name
55
- elsif is_last
56
- names << [fp, :name]
57
- stage = :name
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
- remember = fp
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
- false
176
+ DEFAULT_PREFIXES.include? name_part
143
177
  end
144
178
 
145
179
  def self.valid_suffix? name_part
146
- false
180
+ DEFAULT_SUFFIXES.include? name_part
147
181
  end
148
182
 
149
183
  end
data/spec/database.yml CHANGED
@@ -6,7 +6,7 @@ mysql:
6
6
  adapter: mysql
7
7
  hostname: localhost
8
8
  username: root
9
- password:
9
+ password: msdb3181
10
10
  database: has_person_name
11
11
 
12
12
  postgresql:
@@ -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", nil],
64
+ [nil, "Frans", nil, "van der", "Sluis", "Phd."],
65
65
  [nil, "Maria", "Cornelia Hendrina", nil, "Damen-van Valenberg", nil],
66
- [nil, "Maria", "Cornelia Hendrina", nil, "Damen - van Valenberg", nil],
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: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
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-09-27 00:00:00 +02:00
18
+ date: 2010-10-25 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21