smartname 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +21 -19
- data/VERSION +1 -1
- data/lib/smart_name.rb +32 -223
- data/lib/smart_name/contextual.rb +113 -0
- data/lib/smart_name/manipulate.rb +73 -0
- data/lib/smart_name/parts.rb +124 -0
- data/lib/smart_name/predicates.rb +39 -0
- data/lib/smart_name/variants.rb +36 -0
- data/spec/lib/smart_name/contextual_spec.rb +133 -0
- data/spec/lib/smart_name/manipulate_spec.rb +75 -0
- data/spec/lib/smart_name/parts_spec.rb +65 -0
- data/spec/lib/smart_name/variants_spec.rb +44 -0
- data/spec/lib/smart_name_spec.rb +50 -183
- data/spec/spec_helper.rb +1 -0
- metadata +11 -2
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative "../../spec_helper"
|
3
|
+
|
4
|
+
RSpec.describe SmartName::Parts do
|
5
|
+
describe 'simple?' do
|
6
|
+
it 'returns true for empty name' do
|
7
|
+
expect("".to_name.simple?).to eq true
|
8
|
+
end
|
9
|
+
it 'returns true for simple name' do
|
10
|
+
expect("a name".to_name.simple?).to eq true
|
11
|
+
end
|
12
|
+
it 'returns false for junction name' do
|
13
|
+
expect("A+B".to_name.simple?).to eq false
|
14
|
+
end
|
15
|
+
it 'returns false for junction with empty part' do
|
16
|
+
expect("A+".to_name.simple?).to eq false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'parts and pieces' do
|
21
|
+
it 'produces simple strings for parts' do
|
22
|
+
expect('A+B+C+D'.to_name.parts).to eq(%w( A B C D ))
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'produces simple name objects for part_names' do
|
26
|
+
expect('A+B+C+D'.to_name.part_names).to eq(%w( A B C D ).map(&:to_name))
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'produces compound strings for pieces' do
|
30
|
+
expect('A+B+C+D'.to_name.pieces).to eq(%w( A B C D A+B A+B+C A+B+C+D ))
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'produces compound name objects for piece_names' do
|
34
|
+
expect('A+B+C+D'.to_name.piece_names).to eq(
|
35
|
+
%w( A B C D A+B A+B+C A+B+C+D ).map(&:to_name)
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#left_name' do
|
41
|
+
it 'returns nil for non junction' do
|
42
|
+
expect('a'.to_name.left_name).to eq(nil)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns parent for parent' do
|
46
|
+
expect('a+b+c+d'.to_name.left_name).to eq('a+b+c')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#tag_name' do
|
51
|
+
it 'returns last part of plus card' do
|
52
|
+
expect('a+b+c'.to_name.tag).to eq('c')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'returns name of simple card' do
|
56
|
+
expect('a'.to_name.tag).to eq('a')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'array methods' do
|
61
|
+
it 'flatten preserves empty names' do
|
62
|
+
expect(["".to_name, "A"].flatten.to_name.s).to eq "+A"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative "../../spec_helper"
|
3
|
+
|
4
|
+
RSpec.describe SmartName::Variants do
|
5
|
+
describe '#url_key' do
|
6
|
+
cardnames = [
|
7
|
+
'GrassCommons.org',
|
8
|
+
'Oh you @##',
|
9
|
+
"Alice's Restaurant!",
|
10
|
+
'PB & J',
|
11
|
+
'Mañana'
|
12
|
+
].map(&:to_name)
|
13
|
+
|
14
|
+
cardnames.each do |cardname|
|
15
|
+
it 'should have the same key as the name' do
|
16
|
+
expect(cardname.key).to eq(cardname.url_key.to_name.key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should handle compound names cleanly' do
|
21
|
+
expect('What?+the!+heck$'.to_name.url_key).to eq('What+the+heck')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#safe_key' do
|
26
|
+
it 'subs pluses & stars' do
|
27
|
+
expect('Alpha?+*be-ta'.to_name.safe_key).to eq('alpha-Xbe_tum')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#url_key" do
|
32
|
+
cardnames = ["GrassCommons.org", "Oh you @##", "Alice's Restaurant!",
|
33
|
+
"PB & J", "Mañana"].map(&:to_name)
|
34
|
+
|
35
|
+
cardnames.each do |cardname|
|
36
|
+
it "has the same key as the name" do
|
37
|
+
k = cardname.key
|
38
|
+
k2 = cardname.url_key
|
39
|
+
# warn "cn tok #{cardname.inspect}, #{k.inspect}, #{k2.inspect}"
|
40
|
+
expect(k).to eq(k2.to_name.key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/lib/smart_name_spec.rb
CHANGED
@@ -1,262 +1,129 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
3
|
-
require 'core_ext'
|
4
3
|
|
5
4
|
describe SmartName do
|
6
5
|
describe '#key' do
|
7
6
|
it 'should remove spaces' do
|
8
|
-
'this Name'.to_name.key.
|
7
|
+
expect('this Name'.to_name.key).to eq('this_name')
|
9
8
|
end
|
10
9
|
|
11
10
|
it 'should have initial _ for initial cap' do
|
12
|
-
'This Name'.to_name.key.
|
11
|
+
expect('This Name'.to_name.key).to eq('this_name')
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'should have initial _ for initial cap' do
|
16
|
-
'_This Name'.to_name.key.
|
15
|
+
expect('_This Name'.to_name.key).to eq('this_name')
|
17
16
|
end
|
18
17
|
|
19
18
|
it 'should singularize' do
|
20
|
-
'ethans'.to_name.key.
|
19
|
+
expect('ethans'.to_name.key).to eq('ethan')
|
21
20
|
end
|
22
21
|
|
23
22
|
it 'should underscore' do
|
24
|
-
'ThisThing'.to_name.key.
|
23
|
+
expect('ThisThing'.to_name.key).to eq('this_thing')
|
25
24
|
end
|
26
25
|
|
27
26
|
it 'should handle plus cards' do
|
28
|
-
'ThisThing+Ethans'.to_name.key.
|
27
|
+
expect('ThisThing+Ethans'.to_name.key).to eq('this_thing+ethan')
|
29
28
|
end
|
30
29
|
|
31
30
|
it 'should retain * for star cards' do
|
32
|
-
'*right'.to_name.key.
|
31
|
+
expect('*right'.to_name.key).to eq('*right')
|
33
32
|
end
|
34
33
|
|
35
34
|
it "should not singularize double s's" do
|
36
|
-
'grass'.to_name.key.
|
35
|
+
expect('grass'.to_name.key).to eq('grass')
|
37
36
|
end
|
38
37
|
|
39
38
|
it "should not singularize letter 'S'" do
|
40
|
-
'S'.to_name.key.
|
39
|
+
expect('S'.to_name.key).to eq('s')
|
41
40
|
end
|
42
41
|
|
43
42
|
it 'should handle unicode characters' do
|
44
|
-
'Mañana'.to_name.key.
|
43
|
+
expect('Mañana'.to_name.key).to eq('mañana')
|
45
44
|
end
|
46
45
|
|
47
46
|
it 'should handle weird initial characters' do
|
48
|
-
'__you motha @#$'.to_name.key.
|
49
|
-
'?!_you motha @#$'.to_name.key.
|
47
|
+
expect('__you motha @#$'.to_name.key).to eq('you_motha')
|
48
|
+
expect('?!_you motha @#$'.to_name.key).to eq('you_motha')
|
50
49
|
end
|
51
50
|
|
52
51
|
it 'should allow numbers' do
|
53
|
-
'3way'.to_name.key.
|
52
|
+
expect('3way'.to_name.key).to eq('3way')
|
54
53
|
end
|
55
54
|
|
56
55
|
it 'internal plurals' do
|
57
|
-
'cards hooks label foos'.to_name.key.
|
56
|
+
expect('cards hooks label foos'.to_name.key).to eq('card_hook_label_foo')
|
58
57
|
end
|
59
58
|
|
60
59
|
it 'should handle html entities' do
|
61
60
|
# This no longer takes off the s, is singularize broken now?
|
62
|
-
'Jean-françois Noubel'.to_name.key.
|
61
|
+
expect('Jean-françois Noubel'.to_name.key).to eq('jean_françoi_noubel')
|
63
62
|
end
|
64
63
|
end
|
65
|
-
|
64
|
+
|
66
65
|
describe 'unstable keys' do
|
67
66
|
context 'stabilize' do
|
68
67
|
before do
|
69
68
|
SmartName.stabilize = true
|
70
69
|
end
|
71
70
|
it 'should uninflect until key is stable' do
|
72
|
-
"matthias".to_name.key.
|
71
|
+
expect("matthias".to_name.key).to eq("matthium")
|
73
72
|
end
|
74
73
|
end
|
75
|
-
|
74
|
+
|
76
75
|
context 'do not stabilize' do
|
77
76
|
before do
|
78
77
|
SmartName.stabilize = false
|
79
78
|
end
|
80
79
|
it 'should not uninflect unstable names' do
|
81
|
-
"ilias".to_name.key.
|
80
|
+
expect("ilias".to_name.key).to eq("ilias")
|
82
81
|
end
|
83
82
|
end
|
84
83
|
end
|
85
84
|
|
86
|
-
describe 'parts and pieces' do
|
87
|
-
it 'should produce simple strings for parts' do
|
88
|
-
'A+B+C+D'.to_name.parts.should == %w( A B C D )
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'should produce simple name objects for part_names' do
|
92
|
-
'A+B+C+D'.to_name.part_names.should == %w( A B C D ).map(&:to_name)
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'should produce compound strings for pieces' do
|
96
|
-
'A+B+C+D'.to_name.pieces.should == %w( A B C D A+B A+B+C A+B+C+D )
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'should produce compound name objects for piece_names' do
|
100
|
-
'A+B+C+D'.to_name.piece_names.should ==
|
101
|
-
%w( A B C D A+B A+B+C A+B+C+D ).map(&:to_name)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe '#url_key' do
|
106
|
-
cardnames = [
|
107
|
-
'GrassCommons.org',
|
108
|
-
'Oh you @##',
|
109
|
-
"Alice's Restaurant!",
|
110
|
-
'PB & J',
|
111
|
-
'Mañana'
|
112
|
-
].map(&:to_name)
|
113
|
-
|
114
|
-
cardnames.each do |cardname|
|
115
|
-
it 'should have the same key as the name' do
|
116
|
-
cardname.key.should == cardname.url_key.to_name.key
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'should handle compound names cleanly' do
|
121
|
-
'What?+the!+heck$'.to_name.url_key.should == 'What+the+heck'
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
85
|
describe '#valid' do
|
126
86
|
it 'accepts valid names' do
|
127
|
-
'this+THAT'.to_name.
|
128
|
-
'THE*ONE*AND$!ONLY'.to_name.
|
87
|
+
expect('this+THAT'.to_name).to be_valid
|
88
|
+
expect('THE*ONE*AND$!ONLY'.to_name).to be_valid
|
129
89
|
end
|
130
90
|
|
131
91
|
it 'rejects invalide names' do
|
132
|
-
'Tes~sd'.to_name.
|
133
|
-
'TEST/DDER'.to_name.
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe '#left_name' do
|
138
|
-
it 'returns nil for non junction' do
|
139
|
-
'a'.to_name.left_name.should == nil
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'returns parent for parent' do
|
143
|
-
'a+b+c+d'.to_name.left_name.should == 'a+b+c'
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
describe '#tag_name' do
|
148
|
-
it 'returns last part of plus card' do
|
149
|
-
'a+b+c'.to_name.tag.should == 'c'
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'returns name of simple card' do
|
153
|
-
'a'.to_name.tag.should == 'a'
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
describe '#safe_key' do
|
158
|
-
it 'subs pluses & stars' do
|
159
|
-
'Alpha?+*be-ta'.to_name.safe_key.should == 'alpha-Xbe_tum'
|
92
|
+
expect('Tes~sd'.to_name).not_to be_valid
|
93
|
+
expect('TEST/DDER'.to_name).not_to be_valid
|
160
94
|
end
|
161
95
|
end
|
162
96
|
|
163
|
-
describe '#
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
'a+b'.to_name.replace_part('b', 'x').to_s.should == 'a+x'
|
169
|
-
end
|
170
|
-
it 'replaces two name parts' do
|
171
|
-
'a+b+c' .to_name.replace_part('a+b', 'x').to_s.should == 'x+c'
|
172
|
-
'a+b+c+d'.to_name.replace_part('a+b', 'e+f').to_s.should == 'e+f+c+d'
|
173
|
-
end
|
174
|
-
it "doesn't replace two part tag" do
|
175
|
-
'a+b+c'.to_name.replace_part('b+c', 'x').to_s.should == 'a+b+c'
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe '#to_absolute' do
|
180
|
-
it 'handles _self, _whole, _' do
|
181
|
-
'_self'.to_name.to_absolute('foo').should == 'foo'
|
182
|
-
'_whole'.to_name.to_absolute('foo').should == 'foo'
|
183
|
-
'_'.to_name.to_absolute('foo').should == 'foo'
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'handles _left' do
|
187
|
-
'_left+Z'.to_name.to_absolute('A+B+C').should == 'A+B+Z'
|
188
|
-
end
|
189
|
-
|
190
|
-
it 'handles white space' do
|
191
|
-
'_left + Z'.to_name.to_absolute('A+B+C').should == 'A+B+Z'
|
192
|
-
end
|
193
|
-
|
194
|
-
it 'handles _right' do
|
195
|
-
'_right+bang'.to_name.to_absolute('nutter+butter').should == 'butter+bang'
|
196
|
-
'C+_right'.to_name.to_absolute('B+A').should == 'C+A'
|
197
|
-
end
|
198
|
-
|
199
|
-
it 'handles leading +' do
|
200
|
-
'+bug'.to_name.to_absolute('hum').should == 'hum+bug'
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'handles trailing +' do
|
204
|
-
'bug+'.to_name.to_absolute('tracks').should == 'bug+tracks'
|
205
|
-
end
|
206
|
-
|
207
|
-
it 'handles _(numbers)' do
|
208
|
-
'_1'.to_name.to_absolute('A+B+C').should == 'A'
|
209
|
-
'_1+_2'.to_name.to_absolute('A+B+C').should == 'A+B'
|
210
|
-
'_2+_3'.to_name.to_absolute('A+B+C').should == 'B+C'
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'handles _LLR etc' do
|
214
|
-
'_R'.to_name.to_absolute('A+B+C+D+E').should == 'E'
|
215
|
-
'_L'.to_name.to_absolute('A+B+C+D+E').should == 'A+B+C+D'
|
216
|
-
'_LR'.to_name.to_absolute('A+B+C+D+E').should == 'D'
|
217
|
-
'_LL'.to_name.to_absolute('A+B+C+D+E').should == 'A+B+C'
|
218
|
-
'_LLR'.to_name.to_absolute('A+B+C+D+E').should == 'C'
|
219
|
-
'_LLL'.to_name.to_absolute('A+B+C+D+E').should == 'A+B'
|
220
|
-
'_LLLR'.to_name.to_absolute('A+B+C+D+E').should == 'B'
|
221
|
-
'_LLLL'.to_name.to_absolute('A+B+C+D+E').should == 'A'
|
222
|
-
end
|
223
|
-
|
224
|
-
context 'mismatched requests' do
|
225
|
-
it 'returns _self for _left or _right on simple cards' do
|
226
|
-
'_left+Z'.to_name.to_absolute('A').should == 'A+Z'
|
227
|
-
'_right+Z'.to_name.to_absolute('A').should == 'A+Z'
|
97
|
+
describe '#include?' do
|
98
|
+
context 'A+B+C' do
|
99
|
+
let(:name) { "A+B+CD+EF".to_name }
|
100
|
+
it '"includes "A"' do
|
101
|
+
expect(name.include? ("A")).to be_truthy
|
228
102
|
end
|
229
|
-
|
230
|
-
|
231
|
-
'_1'.to_name.to_absolute('A').should == 'A'
|
232
|
-
'_1+_2'.to_name.to_absolute('A').should == 'A+A'
|
233
|
-
'_2+_3'.to_name.to_absolute('A').should == 'A+A'
|
103
|
+
it '"includes "a"' do
|
104
|
+
expect(name.include? ("a")).to be_truthy
|
234
105
|
end
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
106
|
+
it '"includes "B"' do
|
107
|
+
expect(name.include? ("B")).to be_truthy
|
108
|
+
end
|
109
|
+
it '"includes "A+B"' do
|
110
|
+
expect(name.include? ("A+B")).to be_truthy
|
111
|
+
end
|
112
|
+
it '"includes "CD+EF"' do
|
113
|
+
expect(name.include? ("CD+EF")).to be_truthy
|
114
|
+
end
|
115
|
+
it '"includes "A+B+CD+EF"' do
|
116
|
+
expect(name.include? ("A+B+CD+EF")).to be_truthy
|
117
|
+
end
|
118
|
+
it '"does not include "A+B+C"' do
|
119
|
+
expect(name.include? ("A+B+C")).to be_falsey
|
120
|
+
end
|
121
|
+
it '"does not include "F"' do
|
122
|
+
expect(name.include? ("F")).to be_falsey
|
123
|
+
end
|
124
|
+
it '"does not include "D+EF"' do
|
125
|
+
expect(name.include? ("AD+EF")).to be_falsey
|
245
126
|
end
|
246
127
|
end
|
247
128
|
end
|
248
|
-
|
249
|
-
describe '#to_show' do
|
250
|
-
it 'ignores ignorables' do
|
251
|
-
'you+awe'.to_name.to_show('you').should == '+awe'
|
252
|
-
'me+you+awe'.to_name.to_show('you').should == 'me+awe' #HMMM..... what should this do?
|
253
|
-
'me+you+awe'.to_name.to_show('me' ).should == '+you+awe'
|
254
|
-
'me+you+awe'.to_name.to_show('me','you').should == '+awe'
|
255
|
-
'me+you'.to_name.to_show('me','you').should == 'me+you'
|
256
|
-
'?a?+awe'.to_name.to_show('A').should == '+awe'
|
257
|
-
'+awe'.to_name.to_show().should == '+awe'
|
258
|
-
'+awe'.to_name.to_show(nil).should == '+awe'
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
129
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smartname
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerry Gleason
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -109,7 +109,16 @@ files:
|
|
109
109
|
- VERSION
|
110
110
|
- lib/core_ext.rb
|
111
111
|
- lib/smart_name.rb
|
112
|
+
- lib/smart_name/contextual.rb
|
113
|
+
- lib/smart_name/manipulate.rb
|
114
|
+
- lib/smart_name/parts.rb
|
115
|
+
- lib/smart_name/predicates.rb
|
116
|
+
- lib/smart_name/variants.rb
|
112
117
|
- spec/inflection_helper.rb
|
118
|
+
- spec/lib/smart_name/contextual_spec.rb
|
119
|
+
- spec/lib/smart_name/manipulate_spec.rb
|
120
|
+
- spec/lib/smart_name/parts_spec.rb
|
121
|
+
- spec/lib/smart_name/variants_spec.rb
|
113
122
|
- spec/lib/smart_name_spec.rb
|
114
123
|
- spec/spec_helper.rb
|
115
124
|
homepage: https://github.com/wagn/smartname
|