normalizy 1.0.1 → 1.1.0
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +5 -0
- data/README.md +50 -23
- data/lib/normalizy/config.rb +1 -0
- data/lib/normalizy/extensions.rb +7 -3
- data/lib/normalizy/filters/money.rb +2 -4
- data/lib/normalizy/filters/percent.rb +2 -4
- data/lib/normalizy/filters/slug.rb +15 -0
- data/lib/normalizy/rspec/matcher.rb +4 -10
- data/lib/normalizy/version.rb +1 -1
- data/spec/normalizy/config/add_spec.rb +1 -0
- data/spec/normalizy/config/initialize_spec.rb +1 -0
- data/spec/normalizy/extensions/filters/date_spec.rb +1 -1
- data/spec/normalizy/extensions/filters/slug_spec.rb +12 -0
- data/spec/normalizy/extensions/normalizy_rules_spec.rb +3 -1
- data/spec/normalizy/filters/date_spec.rb +1 -7
- data/spec/normalizy/filters/money_spec.rb +5 -5
- data/spec/normalizy/filters/percent_spec.rb +5 -5
- data/spec/normalizy/filters/slug_spec.rb +24 -0
- data/spec/rails_helper.rb +0 -4
- data/spec/support/db/schema.rb +6 -0
- data/spec/support/models/model_slug.rb +6 -0
- metadata +35 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2706668666c2a0991809c4b3f459510ba7a5ee6a9619841dfb7b2766b046df8a
|
4
|
+
data.tar.gz: 55fc041e85400d342edfd903f1eae8b8803e31bda54bfd04240b5659f3e4e0fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81218fc0bdf00934a6152527418d6b31ca2dbb5033eaabac4e03c55b08d7ec1c50a63d40643180718a916d35b0421e2aacf933d45866b7e4479b7e5fdbff7de0
|
7
|
+
data.tar.gz: a51b09beec0d39852a63b4b0409d3666c4538e22a41d82456cca1bfaabf128b903fcc51afcd822ad65ee62678f611605f9633018edd56838b3c4570fafea9c77
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -60,7 +60,7 @@ By default, the date is treat as `%F` format and as `UTC` time.
|
|
60
60
|
You can change the format using the `format` options:
|
61
61
|
|
62
62
|
```ruby
|
63
|
-
normalizy :birthday, with: date: { format: '%y/%m/%d' }
|
63
|
+
normalizy :birthday, with: { date: { format: '%y/%m/%d' } }
|
64
64
|
|
65
65
|
'84/10/23'
|
66
66
|
# Tue, 23 Oct 1984 00:00:00 UTC +00:00
|
@@ -71,7 +71,7 @@ normalizy :birthday, with: date: { format: '%y/%m/%d' }
|
|
71
71
|
To convert the date on your time zone, just provide the `time_zone` option:
|
72
72
|
|
73
73
|
```ruby
|
74
|
-
normalizy :birthday, with: date: { time_zone:
|
74
|
+
normalizy :birthday, with: { date: { time_zone: Time.zone } }
|
75
75
|
|
76
76
|
'1984-10-23'
|
77
77
|
# Tue, 23 Oct 1984 00:00:00 EDT -04:00
|
@@ -98,7 +98,7 @@ If no configuration is provided, the default message will be `'%{value} is an in
|
|
98
98
|
If your model receive a `Time` or `DateTime`, you can provide `adjust` options to change you time to begin o the day:
|
99
99
|
|
100
100
|
```ruby
|
101
|
-
normalizy :birthday, with: date: { adjust: :begin }
|
101
|
+
normalizy :birthday, with: { date: { adjust: :begin } }
|
102
102
|
|
103
103
|
Tue, 23 Oct 1984 11:30:00 EDT -04:00
|
104
104
|
# Tue, 23 Oct 1984 00:00:00 EDT -04:00
|
@@ -107,7 +107,7 @@ Tue, 23 Oct 1984 11:30:00 EDT -04:00
|
|
107
107
|
Or to the end of the day:
|
108
108
|
|
109
109
|
```ruby
|
110
|
-
normalizy :birthday, with: date: { adjust: :end }
|
110
|
+
normalizy :birthday, with: { date: { adjust: :end } }
|
111
111
|
|
112
112
|
Tue, 23 Oct 1984 00:00:00 EDT -04:00
|
113
113
|
# Tue, 23 Oct 1984 11:59:59 EDT -04:00
|
@@ -130,7 +130,7 @@ The `separator` will be keeped on value to be possible cast the right integer va
|
|
130
130
|
You can change this separator:
|
131
131
|
|
132
132
|
```ruby
|
133
|
-
normalizy :amount, with: money: { separator: ',' }
|
133
|
+
normalizy :amount, with: { money: { separator: ',' } }
|
134
134
|
|
135
135
|
'R$ 42,00'
|
136
136
|
# '42,00'
|
@@ -152,7 +152,7 @@ And if it does not exists, `.` will be used as default.
|
|
152
152
|
You can retrieve the value in *cents* format, use the `type` options as `cents`:
|
153
153
|
|
154
154
|
```ruby
|
155
|
-
normalizy :amount, with: money: { type: :cents }
|
155
|
+
normalizy :amount, with: { money: { type: :cents } }
|
156
156
|
|
157
157
|
'$ 42.00'
|
158
158
|
# '4200'
|
@@ -164,7 +164,7 @@ As you could see on the last example, when using `type: :cents` is important the
|
|
164
164
|
So, you can configure it to avoid the following error:
|
165
165
|
|
166
166
|
```ruby
|
167
|
-
normalizy :amount, with: money: { type: :cents }
|
167
|
+
normalizy :amount, with: { money: { type: :cents } }
|
168
168
|
|
169
169
|
'$ 42.0'
|
170
170
|
# 420
|
@@ -173,14 +173,14 @@ normalizy :amount, with: money: { type: :cents }
|
|
173
173
|
When you parse it back, the value need to be divided by `100` to be presented, but it will result in a value you do not want: `4.2` instead of the original `42.0`. Just provide a `precision`:
|
174
174
|
|
175
175
|
```ruby
|
176
|
-
normalizy :amount, with: money: { precision: 2 }
|
176
|
+
normalizy :amount, with: { money: { precision: 2 } }
|
177
177
|
|
178
178
|
'$ 42.0'
|
179
179
|
# 42.00
|
180
180
|
```
|
181
181
|
|
182
182
|
```ruby
|
183
|
-
normalizy :amount, with: money: { precision: 2, type: :cents }
|
183
|
+
normalizy :amount, with: { money: { precision: 2, type: :cents } }
|
184
184
|
|
185
185
|
'$ 42.0'
|
186
186
|
# 4200
|
@@ -202,7 +202,7 @@ And if it does not exists, `2` will be used as default.
|
|
202
202
|
If you need get a number over a normalized string in a number style, provide `cast` option with desired cast method:
|
203
203
|
|
204
204
|
```ruby
|
205
|
-
normalizy :amount, with: money: { cast: :to_i }
|
205
|
+
normalizy :amount, with: { money: { cast: :to_i } }
|
206
206
|
|
207
207
|
'$ 42.00'
|
208
208
|
# 4200
|
@@ -212,7 +212,7 @@ Just pay attention to avoid to use `type: :cents` together `cast` with float par
|
|
212
212
|
Since `type` runs first, you will add decimal in a number that already is represented with decimal, but as integer:
|
213
213
|
|
214
214
|
```ruby
|
215
|
-
normalizy :amount, with: money: { cast: :to_f, type: :cents }
|
215
|
+
normalizy :amount, with: { money: { cast: :to_f, type: :cents } }
|
216
216
|
|
217
217
|
'$ 42.00'
|
218
218
|
# 4200.0
|
@@ -220,6 +220,8 @@ normalizy :amount, with: money: { cast: :to_f, type: :cents }
|
|
220
220
|
|
221
221
|
### Number
|
222
222
|
|
223
|
+
Transform text to valid number.
|
224
|
+
|
223
225
|
```ruby
|
224
226
|
normalizy :age, with: :number
|
225
227
|
|
@@ -230,7 +232,7 @@ normalizy :age, with: :number
|
|
230
232
|
If you want cast the value, provide `cast` option with desired cast method:
|
231
233
|
|
232
234
|
```ruby
|
233
|
-
normalizy :age, with: number: { cast: :to_i }
|
235
|
+
normalizy :age, with: { number: { cast: :to_i } }
|
234
236
|
|
235
237
|
' 32x'
|
236
238
|
# 32
|
@@ -238,7 +240,7 @@ normalizy :age, with: number: { cast: :to_i }
|
|
238
240
|
|
239
241
|
### Percent
|
240
242
|
|
241
|
-
Transform a value to percent format.
|
243
|
+
Transform a value to a valid percent format.
|
242
244
|
|
243
245
|
```ruby
|
244
246
|
normalizy :amount, with: :percent
|
@@ -253,7 +255,7 @@ The `separator` will be keeped on value to be possible cast the right integer va
|
|
253
255
|
You can change this separator:
|
254
256
|
|
255
257
|
```ruby
|
256
|
-
normalizy :amount, with: percent: { separator: ',' }
|
258
|
+
normalizy :amount, with: { percent: { separator: ',' } }
|
257
259
|
|
258
260
|
'42,00 %'
|
259
261
|
# '42,00'
|
@@ -275,7 +277,7 @@ And if it does not exists, `.` will be used as default.
|
|
275
277
|
You can retrieve the value in *integer* format, use the `type` options as `integer`:
|
276
278
|
|
277
279
|
```ruby
|
278
|
-
normalizy :amount, with: percent: { type: :integer }
|
280
|
+
normalizy :amount, with: { percent: { type: :integer } }
|
279
281
|
|
280
282
|
'42.00 %'
|
281
283
|
# '4200'
|
@@ -287,7 +289,7 @@ As you could see on the last example, when using `type: :integer` is important t
|
|
287
289
|
So, you can configure it to avoid the following error:
|
288
290
|
|
289
291
|
```ruby
|
290
|
-
normalizy :amount, with: percent: { type: :integer }
|
292
|
+
normalizy :amount, with: { percent: { type: :integer } }
|
291
293
|
|
292
294
|
'42.0 %'
|
293
295
|
# 420
|
@@ -296,14 +298,14 @@ normalizy :amount, with: percent: { type: :integer }
|
|
296
298
|
When you parse it back, the value need to be divided by `100` to be presented, but it will result in a value you do not want: `4.2` instead of the original `42.0`. Just provide a `precision`:
|
297
299
|
|
298
300
|
```ruby
|
299
|
-
normalizy :amount, with: percent: { precision: 2 }
|
301
|
+
normalizy :amount, with: { percent: { precision: 2 } }
|
300
302
|
|
301
303
|
'42.0 %'
|
302
304
|
# 42.00
|
303
305
|
```
|
304
306
|
|
305
307
|
```ruby
|
306
|
-
normalizy :amount, with: percent: { precision: 2, type: :integer }
|
308
|
+
normalizy :amount, with: { percent: { precision: 2, type: :integer } }
|
307
309
|
|
308
310
|
'42.0 %'
|
309
311
|
# 4200
|
@@ -325,7 +327,7 @@ And if it does not exists, `2` will be used as default.
|
|
325
327
|
If you need get a number over a normalized string in a number style, provide `cast` option with desired cast method:
|
326
328
|
|
327
329
|
```ruby
|
328
|
-
normalizy :amount, with: percent: { cast: :to_i }
|
330
|
+
normalizy :amount, with: { percent: { cast: :to_i } }
|
329
331
|
|
330
332
|
'42.00 %'
|
331
333
|
# 4200
|
@@ -335,14 +337,39 @@ Just pay attention to avoid to use `type: :integer` together `cast` with float p
|
|
335
337
|
Since `type` runs first, you will add decimal in a number that already is represented with decimal, but as integer:
|
336
338
|
|
337
339
|
```ruby
|
338
|
-
normalizy :amount, with: percent: { cast: :to_f, type: :integer }
|
340
|
+
normalizy :amount, with: { percent: { cast: :to_f, type: :integer } }
|
339
341
|
|
340
342
|
'42.00 %'
|
341
343
|
# 4200.0
|
342
344
|
```
|
343
345
|
|
346
|
+
### Slug
|
347
|
+
|
348
|
+
Convert texto to slug.
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
normalizy :slug, with: :slug
|
352
|
+
'Washington é Botelho'
|
353
|
+
# 'washington-e-botelho'
|
354
|
+
```
|
355
|
+
|
356
|
+
#### to
|
357
|
+
|
358
|
+
You can slug a field based on other just sending the result value.
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
normalizy :title, with: { slug: { to: :slug } }
|
362
|
+
|
363
|
+
model.title = 'Washington é Botelho'
|
364
|
+
|
365
|
+
model.slug
|
366
|
+
# 'washington-e-botelho'
|
367
|
+
```
|
368
|
+
|
344
369
|
### Strip
|
345
370
|
|
371
|
+
Cleans edge spaces.
|
372
|
+
|
346
373
|
Options:
|
347
374
|
|
348
375
|
- `side`: `:left`, `:right` or `:both`. Default: `:both`
|
@@ -378,7 +405,7 @@ As you can see, the rules can be passed as Symbol/String or as Hash if it has op
|
|
378
405
|
You can normalize with a couple of filters at once:
|
379
406
|
|
380
407
|
```ruby
|
381
|
-
normalizy :name, with: %i[squish titleize]
|
408
|
+
normalizy :name, with: { %i[squish titleize] }
|
382
409
|
' washington botelho '
|
383
410
|
# 'Washington Botelho'
|
384
411
|
```
|
@@ -482,7 +509,7 @@ end
|
|
482
509
|
```
|
483
510
|
|
484
511
|
```ruby
|
485
|
-
normalizy :name, with: blacklist: { replacement: '---' }
|
512
|
+
normalizy :name, with: { blacklist: { replacement: '---' } }
|
486
513
|
|
487
514
|
'Washington Fuck Botelho'
|
488
515
|
# 'Washington --- Botelho'
|
@@ -514,7 +541,7 @@ end
|
|
514
541
|
```
|
515
542
|
|
516
543
|
```ruby
|
517
|
-
normalizy :name, with: :blacklist, &->(value) { value.sub('filtered', '(filtered 2x)') }
|
544
|
+
normalizy :name, with: { :blacklist, &->(value) { value.sub('filtered', '(filtered 2x)') } }
|
518
545
|
|
519
546
|
'Washington Fuck Botelho'
|
520
547
|
# 'Washington (filtered 2x) Botelho'
|
data/lib/normalizy/config.rb
CHANGED
data/lib/normalizy/extensions.rb
CHANGED
@@ -56,8 +56,8 @@ module Normalizy
|
|
56
56
|
[rules].flatten.compact.each do |rule|
|
57
57
|
rule_name, rule_options = extract_rule(rule)
|
58
58
|
|
59
|
-
unalias_for(rule_name).each do |
|
60
|
-
filter, filter_options = extract_filter(
|
59
|
+
unalias_for(rule_name).each do |unaliased_rule|
|
60
|
+
filter, filter_options = extract_filter(unaliased_rule, rule_options, attribute)
|
61
61
|
result = extract_value(result, filter, filter_options, block)
|
62
62
|
end
|
63
63
|
end
|
@@ -95,7 +95,11 @@ module Normalizy
|
|
95
95
|
value: value
|
96
96
|
)
|
97
97
|
|
98
|
-
|
98
|
+
if rules.is_a?(Hash) && rules.dig(:slug, :to).present?
|
99
|
+
write_attribute rules.dig(:slug, :to), result
|
100
|
+
else
|
101
|
+
super result
|
102
|
+
end
|
99
103
|
end
|
100
104
|
end
|
101
105
|
}
|
@@ -12,9 +12,7 @@ module Normalizy
|
|
12
12
|
return nil if value.blank?
|
13
13
|
|
14
14
|
if cents?(options)
|
15
|
-
if value.include? separator(options)
|
16
|
-
value = precisioned(value, options).delete('.')
|
17
|
-
end
|
15
|
+
value = precisioned(value, options).delete('.') if value.include? separator(options)
|
18
16
|
else
|
19
17
|
value = precisioned(value, options)
|
20
18
|
end
|
@@ -35,7 +33,7 @@ module Normalizy
|
|
35
33
|
end
|
36
34
|
|
37
35
|
def precisioned(value, options)
|
38
|
-
"%0.#{precision(options)}f"
|
36
|
+
format("%0.#{precision(options)}f", value.sub(separator(options), '.'))
|
39
37
|
end
|
40
38
|
|
41
39
|
def separator(options)
|
@@ -12,9 +12,7 @@ module Normalizy
|
|
12
12
|
return nil if value.blank?
|
13
13
|
|
14
14
|
if cents?(options)
|
15
|
-
if value.include? separator(options)
|
16
|
-
value = precisioned(value, options).delete('.')
|
17
|
-
end
|
15
|
+
value = precisioned(value, options).delete('.') if value.include? separator(options)
|
18
16
|
else
|
19
17
|
value = precisioned(value, options)
|
20
18
|
end
|
@@ -35,7 +33,7 @@ module Normalizy
|
|
35
33
|
end
|
36
34
|
|
37
35
|
def precisioned(value, options)
|
38
|
-
"%0.#{precision(options)}f"
|
36
|
+
format("%0.#{precision(options)}f", value.sub(separator(options), '.'))
|
39
37
|
end
|
40
38
|
|
41
39
|
def separator(options)
|
@@ -43,9 +43,7 @@ module Normalizy
|
|
43
43
|
|
44
44
|
return false if options.blank?
|
45
45
|
|
46
|
-
if options.map { |option| option[:rules] }.compact.blank?
|
47
|
-
options = default_rules
|
48
|
-
end
|
46
|
+
options = default_rules if options.map { |option| option[:rules] }.compact.blank?
|
49
47
|
|
50
48
|
return false if options.blank?
|
51
49
|
|
@@ -113,13 +111,9 @@ module Normalizy
|
|
113
111
|
result = options.map do |option|
|
114
112
|
rules = option[:rules]
|
115
113
|
|
116
|
-
if rules.nil?
|
117
|
-
|
118
|
-
|
119
|
-
%("#{rules}")
|
120
|
-
else
|
121
|
-
rules
|
122
|
-
end
|
114
|
+
return :nil if rules.nil?
|
115
|
+
|
116
|
+
rules.presence || %("#{rules}")
|
123
117
|
end
|
124
118
|
|
125
119
|
result.join ', '
|
data/lib/normalizy/version.rb
CHANGED
@@ -16,7 +16,7 @@ RSpec.describe ModelDate, 'filters:date' do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
specify do
|
19
|
-
hours =
|
19
|
+
hours = ActiveSupport::TimeZone['Brasilia'].utc_offset / 3600.0
|
20
20
|
expected = Time.new(1984, 10, 23, 0, 0, 0, 0) + (hours.hours * -1)
|
21
21
|
|
22
22
|
expect(described_class.create(date_time_zone: '1984-10-23').date_time_zone).to eq expected
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe ModelSlug, 'filters:slug' do
|
6
|
+
from = 'The Títle'
|
7
|
+
to = 'the-title'
|
8
|
+
|
9
|
+
specify { expect(described_class.create(permalink: from).permalink).to eq to }
|
10
|
+
|
11
|
+
specify { expect(described_class.create(title: from).slug).to eq to }
|
12
|
+
end
|
@@ -27,9 +27,11 @@ RSpec.describe Normalizy::Extension, ':normalizy_rules' do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
specify do
|
30
|
+
expected = { name: [{ block: nil, options: {}, rules: [:upcase, 'blank', { trim: { side: :left } }] }] }
|
31
|
+
|
30
32
|
model.normalizy :name, with: [:upcase, 'blank', { trim: { side: :left } }]
|
31
33
|
|
32
|
-
expect(model.normalizy_rules).to eq
|
34
|
+
expect(model.normalizy_rules).to eq expected
|
33
35
|
end
|
34
36
|
|
35
37
|
specify do
|
@@ -9,8 +9,6 @@ RSpec.describe Normalizy::Filters::Date do
|
|
9
9
|
|
10
10
|
it { expect(subject.call('84/10/23', format: '%y/%m/%d')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }
|
11
11
|
|
12
|
-
it { expect(subject.call('84/10/23', format: '%y/%m/%d')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }
|
13
|
-
|
14
12
|
it { expect(subject.call(Time.new(1984, 10, 23), adjust: :end)).to eq Time.new(1984, 10, 23).end_of_day }
|
15
13
|
it { expect(subject.call(Time.new(1984, 10, 23, 1), adjust: :begin)).to eq Time.new(1984, 10, 23).beginning_of_day }
|
16
14
|
|
@@ -29,12 +27,10 @@ RSpec.describe Normalizy::Filters::Date do
|
|
29
27
|
allow(I18n).to receive(:t).with(:date,
|
30
28
|
scope: ['normalizy.errors.date', 'model_date'],
|
31
29
|
value: '1984-10-00',
|
32
|
-
default: '%{value} is an invalid date.')
|
30
|
+
default: '%{value} is an invalid date.').and_return 'date.error'
|
33
31
|
end
|
34
32
|
|
35
33
|
it 'writes an error on object and does not set the values' do
|
36
|
-
expected = Time.new(1984, 10, 23, 0, 0, 0, 0)
|
37
|
-
|
38
34
|
subject.call '1984-10-00', options
|
39
35
|
|
40
36
|
expect(object.errors[:date]).to eq ['date.error']
|
@@ -44,8 +40,6 @@ RSpec.describe Normalizy::Filters::Date do
|
|
44
40
|
|
45
41
|
context 'with no I18n present' do
|
46
42
|
it 'writes a default error on object and does not set the values' do
|
47
|
-
expected = Time.new(1984, 10, 23, 0, 0, 0, 0)
|
48
|
-
|
49
43
|
subject.call '1984-10-00', options
|
50
44
|
|
51
45
|
expect(object.errors[:date]).to eq ['1984-10-00 is an invalid date.']
|
@@ -103,14 +103,14 @@ RSpec.describe Normalizy::Filters::Money do
|
|
103
103
|
end
|
104
104
|
|
105
105
|
describe 'separator' do
|
106
|
-
context 'provided inline' do
|
106
|
+
context 'when provided inline' do
|
107
107
|
it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
|
108
108
|
end
|
109
109
|
|
110
|
-
context 'provided I18n' do
|
110
|
+
context 'when provided I18n' do
|
111
111
|
before do
|
112
|
-
allow(I18n).to receive(:t).with('currency.format.separator', default: '.')
|
113
|
-
allow(I18n).to receive(:t).with('currency.format.precision', default: 2)
|
112
|
+
allow(I18n).to receive(:t).with('currency.format.separator', default: '.').and_return 'x'
|
113
|
+
allow(I18n).to receive(:t).with('currency.format.precision', default: 2).and_return 2
|
114
114
|
end
|
115
115
|
|
116
116
|
it { expect(subject.call('1x2')).to eq '1.20' }
|
@@ -139,7 +139,7 @@ RSpec.describe Normalizy::Filters::Money do
|
|
139
139
|
it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
|
140
140
|
it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
|
141
141
|
|
142
|
-
context '
|
142
|
+
context 'with calls cast' do
|
143
143
|
it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
|
144
144
|
it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
|
145
145
|
it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
|
@@ -103,14 +103,14 @@ RSpec.describe Normalizy::Filters::Percent do
|
|
103
103
|
end
|
104
104
|
|
105
105
|
describe 'separator' do
|
106
|
-
context 'provided inline' do
|
106
|
+
context 'when provided inline' do
|
107
107
|
it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
|
108
108
|
end
|
109
109
|
|
110
|
-
context 'provided I18n' do
|
110
|
+
context 'when provided I18n' do
|
111
111
|
before do
|
112
|
-
allow(I18n).to receive(:t).with('percentage.format.separator', default: '.')
|
113
|
-
allow(I18n).to receive(:t).with('percentage.format.precision', default: 2)
|
112
|
+
allow(I18n).to receive(:t).with('percentage.format.separator', default: '.').and_return 'x'
|
113
|
+
allow(I18n).to receive(:t).with('percentage.format.precision', default: 2).and_return 2
|
114
114
|
end
|
115
115
|
|
116
116
|
it { expect(subject.call('1x2')).to eq '1.20' }
|
@@ -139,7 +139,7 @@ RSpec.describe Normalizy::Filters::Percent do
|
|
139
139
|
it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
|
140
140
|
it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
|
141
141
|
|
142
|
-
context '
|
142
|
+
context 'with calls cast' do
|
143
143
|
it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
|
144
144
|
it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
|
145
145
|
it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe Normalizy::Filters::Slug do
|
6
|
+
describe 'default options' do
|
7
|
+
it { expect(subject.call(nil)).to eq nil }
|
8
|
+
it { expect(subject.call('')).to eq '' }
|
9
|
+
|
10
|
+
it { expect(subject.call('The Title')).to eq 'the-title' }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'to' do
|
14
|
+
it { expect(subject.call(nil , to: :slug)).to eq nil }
|
15
|
+
it { expect(subject.call('' , to: :slug)).to eq '' }
|
16
|
+
it { expect(subject.call('The Title', to: :slug)).to eq 'the-title' }
|
17
|
+
|
18
|
+
context 'when is not :to key' do
|
19
|
+
it { expect(subject.call(nil , from: :slug)).to eq nil }
|
20
|
+
it { expect(subject.call('' , from: :slug)).to eq '' }
|
21
|
+
it { expect(subject.call('The Title', from: :slug)).to eq 'the-title' }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/rails_helper.rb
CHANGED
data/spec/support/db/schema.rb
CHANGED
@@ -63,6 +63,12 @@ ActiveRecord::Schema.define(version: 0) do
|
|
63
63
|
t.integer :cents_type_and_cast_to_i
|
64
64
|
end
|
65
65
|
|
66
|
+
create_table :model_slugs do |t|
|
67
|
+
t.string :permalink
|
68
|
+
t.string :slug
|
69
|
+
t.string :title
|
70
|
+
end
|
71
|
+
|
66
72
|
create_table :model_strips do |t|
|
67
73
|
t.string :strip
|
68
74
|
t.string :strip_side_both
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: normalizy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '6'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: pry-byebug
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
@@ -58,20 +58,6 @@ dependencies:
|
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
|
-
- !ruby/object:Gem::Dependency
|
62
|
-
name: rubocop-rspec
|
63
|
-
requirement: !ruby/object:Gem::Requirement
|
64
|
-
requirements:
|
65
|
-
- - ">="
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: '0'
|
68
|
-
type: :development
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: !ruby/object:Gem::Requirement
|
71
|
-
requirements:
|
72
|
-
- - ">="
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
version: '0'
|
75
61
|
- !ruby/object:Gem::Dependency
|
76
62
|
name: rubocop
|
77
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,7 +73,7 @@ dependencies:
|
|
87
73
|
- !ruby/object:Gem::Version
|
88
74
|
version: '0'
|
89
75
|
- !ruby/object:Gem::Dependency
|
90
|
-
name:
|
76
|
+
name: rubocop-rspec
|
91
77
|
requirement: !ruby/object:Gem::Requirement
|
92
78
|
requirements:
|
93
79
|
- - ">="
|
@@ -101,7 +87,7 @@ dependencies:
|
|
101
87
|
- !ruby/object:Gem::Version
|
102
88
|
version: '0'
|
103
89
|
- !ruby/object:Gem::Dependency
|
104
|
-
name:
|
90
|
+
name: sqlite3
|
105
91
|
requirement: !ruby/object:Gem::Requirement
|
106
92
|
requirements:
|
107
93
|
- - ">="
|
@@ -133,6 +119,7 @@ files:
|
|
133
119
|
- lib/normalizy/filters/money.rb
|
134
120
|
- lib/normalizy/filters/number.rb
|
135
121
|
- lib/normalizy/filters/percent.rb
|
122
|
+
- lib/normalizy/filters/slug.rb
|
136
123
|
- lib/normalizy/filters/strip.rb
|
137
124
|
- lib/normalizy/rspec/matcher.rb
|
138
125
|
- lib/normalizy/version.rb
|
@@ -145,6 +132,7 @@ files:
|
|
145
132
|
- spec/normalizy/extensions/filters/money_spec.rb
|
146
133
|
- spec/normalizy/extensions/filters/number_spec.rb
|
147
134
|
- spec/normalizy/extensions/filters/percent_spec.rb
|
135
|
+
- spec/normalizy/extensions/filters/slug_spec.rb
|
148
136
|
- spec/normalizy/extensions/filters/strip_spec.rb
|
149
137
|
- spec/normalizy/extensions/model_spec.rb
|
150
138
|
- spec/normalizy/extensions/normalizy_rules_spec.rb
|
@@ -152,6 +140,7 @@ files:
|
|
152
140
|
- spec/normalizy/filters/money_spec.rb
|
153
141
|
- spec/normalizy/filters/number_spec.rb
|
154
142
|
- spec/normalizy/filters/percent_spec.rb
|
143
|
+
- spec/normalizy/filters/slug_spec.rb
|
155
144
|
- spec/normalizy/filters/strip_spec.rb
|
156
145
|
- spec/normalizy/normalizy/configure_spec.rb
|
157
146
|
- spec/normalizy/rspec/matcher/description_spec.rb
|
@@ -173,6 +162,7 @@ files:
|
|
173
162
|
- spec/support/models/model_money.rb
|
174
163
|
- spec/support/models/model_number.rb
|
175
164
|
- spec/support/models/model_percent.rb
|
165
|
+
- spec/support/models/model_slug.rb
|
176
166
|
- spec/support/models/model_strip.rb
|
177
167
|
- spec/support/models/rule.rb
|
178
168
|
homepage: https://github.com/wbotelhos/normalizy
|
@@ -195,47 +185,50 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
185
|
version: '0'
|
196
186
|
requirements: []
|
197
187
|
rubyforge_project:
|
198
|
-
rubygems_version: 2.
|
188
|
+
rubygems_version: 2.7.3
|
199
189
|
signing_key:
|
200
190
|
specification_version: 4
|
201
191
|
summary: Attribute normalizer for ActiveRecord.
|
202
192
|
test_files:
|
203
|
-
- spec/normalizy/
|
204
|
-
- spec/normalizy/
|
205
|
-
- spec/normalizy/
|
193
|
+
- spec/normalizy/filters/date_spec.rb
|
194
|
+
- spec/normalizy/filters/number_spec.rb
|
195
|
+
- spec/normalizy/filters/money_spec.rb
|
196
|
+
- spec/normalizy/filters/percent_spec.rb
|
197
|
+
- spec/normalizy/filters/slug_spec.rb
|
198
|
+
- spec/normalizy/filters/strip_spec.rb
|
206
199
|
- spec/normalizy/config/initialize_spec.rb
|
207
200
|
- spec/normalizy/config/normalizy_aliases_spec.rb
|
201
|
+
- spec/normalizy/config/alias_spec.rb
|
202
|
+
- spec/normalizy/config/add_spec.rb
|
203
|
+
- spec/normalizy/config/default_filters_spec.rb
|
208
204
|
- spec/normalizy/extensions/filters/date_spec.rb
|
209
|
-
- spec/normalizy/extensions/filters/money_spec.rb
|
210
205
|
- spec/normalizy/extensions/filters/number_spec.rb
|
206
|
+
- spec/normalizy/extensions/filters/money_spec.rb
|
211
207
|
- spec/normalizy/extensions/filters/percent_spec.rb
|
208
|
+
- spec/normalizy/extensions/filters/slug_spec.rb
|
212
209
|
- spec/normalizy/extensions/filters/strip_spec.rb
|
213
|
-
- spec/normalizy/extensions/model_spec.rb
|
214
210
|
- spec/normalizy/extensions/normalizy_rules_spec.rb
|
215
|
-
- spec/normalizy/
|
216
|
-
- spec/normalizy/filters/money_spec.rb
|
217
|
-
- spec/normalizy/filters/number_spec.rb
|
218
|
-
- spec/normalizy/filters/percent_spec.rb
|
219
|
-
- spec/normalizy/filters/strip_spec.rb
|
211
|
+
- spec/normalizy/extensions/model_spec.rb
|
220
212
|
- spec/normalizy/normalizy/configure_spec.rb
|
221
|
-
- spec/normalizy/rspec/matcher/
|
213
|
+
- spec/normalizy/rspec/matcher/matches_spec.rb
|
222
214
|
- spec/normalizy/rspec/matcher/failure_message_spec.rb
|
223
215
|
- spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb
|
224
|
-
- spec/normalizy/rspec/matcher/from_spec.rb
|
225
|
-
- spec/normalizy/rspec/matcher/matches_spec.rb
|
226
216
|
- spec/normalizy/rspec/matcher/to_spec.rb
|
227
|
-
- spec/
|
228
|
-
- spec/
|
229
|
-
- spec/support/db/schema.rb
|
230
|
-
- spec/support/filters/blacklist.rb
|
217
|
+
- spec/normalizy/rspec/matcher/from_spec.rb
|
218
|
+
- spec/normalizy/rspec/matcher/description_spec.rb
|
231
219
|
- spec/support/filters/block.rb
|
220
|
+
- spec/support/filters/blacklist.rb
|
232
221
|
- spec/support/filters/info.rb
|
233
|
-
- spec/support/models/
|
222
|
+
- spec/support/models/model_money.rb
|
234
223
|
- spec/support/models/match.rb
|
235
|
-
- spec/support/models/model.rb
|
236
224
|
- spec/support/models/model_date.rb
|
237
|
-
- spec/support/models/
|
238
|
-
- spec/support/models/
|
225
|
+
- spec/support/models/model.rb
|
226
|
+
- spec/support/models/model_slug.rb
|
227
|
+
- spec/support/models/rule.rb
|
239
228
|
- spec/support/models/model_percent.rb
|
229
|
+
- spec/support/models/model_number.rb
|
240
230
|
- spec/support/models/model_strip.rb
|
241
|
-
- spec/support/models/
|
231
|
+
- spec/support/models/alias.rb
|
232
|
+
- spec/support/common.rb
|
233
|
+
- spec/support/db/schema.rb
|
234
|
+
- spec/rails_helper.rb
|