2
2
3
3
[ ![ Build Status] ( https://travis-ci.org/wbotelhos/normalizy.svg )] ( https://travis-ci.org/wbotelhos/normalizy )
4
4
[ ![ Gem Version] ( https://badge.fury.io/rb/normalizy.svg )] ( https://badge.fury.io/rb/normalizy )
5
+ [ ![ Gratipay] ( https://img.shields.io/gratipay/user/wbotelhos.svg )] ( https://gratipay.com/normalizy )
5
6
6
7
Attribute normalizer for ActiveRecord.
7
8
@@ -112,8 +113,6 @@ Tue, 23 Oct 1984 00:00:00 EDT -04:00
112
113
# Tue, 23 Oct 1984 11:59:59 EDT -04:00
113
114
```
114
115
115
- By default, `number` works with value before [Type Cast](#type-cast).
116
-
117
116
### Money
118
117
119
118
Transform a value to money format.
@@ -219,8 +218,6 @@ normalizy :amount, with: money: { cast: :to_f, type: :cents }
219
218
# 4200.0
220
219
` ` `
221
220
222
- By default, `money` works with value before [Type Cast](#type-cast).
223
-
224
221
# ## Number
225
222
226
223
` ` ` ruby
@@ -239,8 +236,6 @@ normalizy :age, with: number: { cast: :to_i }
239
236
# 32
240
237
` ` `
241
238
242
- By default, `number` works with value before [Type Cast](#type-cast).
243
-
244
239
# ## Percent
245
240
246
241
Transform a value to percent format.
@@ -346,8 +341,6 @@ normalizy :amount, with: percent: { cast: :to_f, type: :integer }
346
341
# 4200.0
347
342
` ` `
348
343
349
- By default, `percent` works with value before [Type Cast](#type-cast).
350
-
351
344
# ## Strip
352
345
353
346
Options :
@@ -392,33 +385,40 @@ normalizy :name, with: %i[squish titleize]
392
385
393
386
# # Multiple Attributes
394
387
395
- You can normalize more than one attribute at once too, with one or muiltiple filters :
388
+ You can normalize more than one attribute at once too, with one or multiple filters :
396
389
397
390
` ` ` ruby
398
391
normalizy :email, :username, with: :downcase
399
392
` ` `
400
393
401
- Of course you can declare muiltiples attribute and multiple filters, either.
402
- It is possible to make sequential normalizy calls :
394
+ Of course you can declare multiple attribute and multiple filters, either.
395
+ It is possible to make sequential normalizy calls, but *take care*!
396
+ Since we use `prepend` module the last line will run first then others :
403
397
404
398
` ` ` ruby
405
- normalizy :email, :username, with: :squish
406
- normalizy :email , with: :downcase
399
+ normalizy :username, with: :downcase
400
+ normalizy :username, with: :titleize
401
+
402
+ 'BoteLho'
403
+ # 'bote lho'
407
404
` ` `
408
405
409
- In this case, each line will be evaluated from the top to the bottom.
406
+ As you can see, `titleize` runs first then `downcase`.
407
+ Each line will be evaluated from the *bottom* to the *top*.
408
+ If it is hard to you accept, use [Muiltiple Filters](#multiple-filters)
410
409
411
410
# # Default Filters
412
411
413
- You can configure some default filters to be runned. Edit initializer at `config/initializers/normalizy.rb` :
412
+ You can configure some default filters to be runned.
413
+ Edit initializer at `config/initializers/normalizy.rb` :
414
414
415
415
` ` ` ruby
416
416
Normalizy.configure do |config|
417
417
config.default_filters = [:squish]
418
418
end
419
419
` ` `
420
420
421
- Now, all normalization will include squish, even when no rule is declared.
421
+ Now, all normalization will include ` squish` , even when no rule is declared.
422
422
423
423
` ` ` ruby
424
424
normalizy :name
@@ -580,37 +580,6 @@ Normalizy.configure do |config|
580
580
end
581
581
` ` `
582
582
583
- # # Type Cast
584
-
585
- An input field with `= 42` value when sent to model with a field as `integer` type,
586
- will be converted to `0`, since the type does not match. But you want to use the value before Rails cast the type.
587
-
588
- To receive the value before type cast, just pass a `raw` options as `true` :
589
-
590
- ` ` ` ruby
591
- normalizy :amount, with: :number, raw: true
592
-
593
- '= 42'
594
- # 42
595
- ` ` `
596
-
597
- To avoid repeat the `raw : true` when you have multiple uses, you can register a filter:
598
-
599
- ` ` ` ruby
600
- Normalizy.configure do |config|
601
- config.add :raw_number, ->(input) { input.gsub(/\D /, '') }, raw: true
602
- end
603
- ` ` `
604
-
605
- And use it in short version :
606
-
607
- ` ` ` ruby
608
- normalizy :amount, with: :raw_number
609
-
610
- '= 42'
611
- # 42
612
- ` ` `
613
-
614
583
# # Alias
615
584
616
585
Sometimes you want to give a better name to your filter, just to keep the things semantic.
623
592
` ` `
624
593
625
594
Now, `age` will delegate to `number` filter.
626
- Since we already know the need of `raw` options, we can declare it here :
627
-
628
- ` ` ` ruby
629
- Normalizy.configure do |config|
630
- config.alias :age, :number, raw: true
631
- end
632
- ` ` `
633
595
634
596
And now, the aliased filter will work fine :
635
597
@@ -648,6 +610,14 @@ Normalizy.configure do |config|
648
610
end
649
611
` ` `
650
612
613
+ Alias accepts options parameters too :
614
+
615
+ ` ` ` ruby
616
+ Normalizy.configure do |config|
617
+ config.alias :left_trim, trim: { side: :left }
618
+ end
619
+ ` ` `
620
+
651
621
# # RSpec
652
622
653
623
If you use [RSpec](http://rspec.info), we did built-in matchers for you.
@@ -685,4 +655,4 @@ it { is_expected.to normalizy(:email).with(trim: { side: :left }) }
685
655
686
656
# # Love it!
687
657
688
- Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=normalizy) or [Gratipay](https://gratipay.com/~wbotelhos ). Thanks! ( :
658
+ Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=normalizy) or [Gratipay](https://gratipay.com/normalizy ). Thanks! ( :
0 commit comments