simple_model 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_model/extend_core.rb +11 -2
- data/lib/simple_model/version.rb +1 -1
- data/spec/extend_core_spec.rb +16 -0
- metadata +1 -1
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
module ExtendCore
|
2
3
|
require 'time'
|
3
4
|
require 'date'
|
@@ -124,7 +125,15 @@ module ExtendCore
|
|
124
125
|
# +seperate_middle_name+ defaults to true. if false, will combine middle name into last name.
|
125
126
|
|
126
127
|
def parse_name(seperate_middle_name=true)
|
127
|
-
|
128
|
+
str = self
|
129
|
+
if str.include?(',') # Rearrange names formatted as Doe, John C. to John C. Doe
|
130
|
+
temp = str.split(',')
|
131
|
+
temp << temp[0]
|
132
|
+
temp.delete_at(0)
|
133
|
+
str = temp.join(" ")
|
134
|
+
|
135
|
+
end
|
136
|
+
parts = str.split # First, split the name into an array
|
128
137
|
|
129
138
|
parts.each_with_index do |part, i|
|
130
139
|
# If any part is "and", then put together the two parts around it
|
@@ -136,7 +145,7 @@ module ExtendCore
|
|
136
145
|
|
137
146
|
{ :prefix => (parts.shift if parts[0]=~/^\w+\./),
|
138
147
|
:first_name => parts.shift || "", # if name is "", then atleast first_name should be ""
|
139
|
-
:suffix => (parts.pop if parts[-1]=~/(\w+\.|[IVXLM]+|[A-Z]
|
148
|
+
:suffix => (parts.pop if parts[-1]=~/(\w+\.|[IVXLM]+|[A-Z]+\.|(?i)jr|(?i)sr )$/),
|
140
149
|
:last_name => (seperate_middle_name ? parts.pop : parts.slice!(0..-1) * " "),
|
141
150
|
:middle_name => (parts * " " unless parts.empty?) }
|
142
151
|
end
|
data/lib/simple_model/version.rb
CHANGED
data/spec/extend_core_spec.rb
CHANGED
@@ -83,4 +83,20 @@ describe ExtendCore, 'String.rb' do
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
+
describe ExtendCore, 'parse_name' do
|
87
|
+
it "return return hash with name elements properly keyed" do
|
88
|
+
hash = "Doe, John C.".parse_name
|
89
|
+
hash[:first_name].should eql("John")
|
90
|
+
hash[:last_name].should eql("Doe")
|
91
|
+
|
92
|
+
hash = "John Doe".parse_name
|
93
|
+
hash[:first_name].should eql("John")
|
94
|
+
hash[:last_name].should eql("Doe")
|
95
|
+
|
96
|
+
hash = "Mr. John C. Doe Jr".parse_name
|
97
|
+
hash[:first_name].should eql("John")
|
98
|
+
hash[:last_name].should eql("Doe")
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
86
102
|
end
|