Skip to content

Commit 0d0a08a

Browse files
committed
v1.0.0
1 parent 9e540e1 commit 0d0a08a

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## v1.0.0
2+
3+
- Changes:
4+
- `raw` options was dropped since `prepend` already works before type cast;
5+
- Multiple lines of `normalizy` will be evaluated from the bottom to top.
6+
7+
- Features:
8+
- `alias` configuration now accepts options;
9+
10+
- Fixes:
11+
- Multiple filters were not running together.
12+
113
## v0.2.0
214

315
- Changes:

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
normalizy (0.2.0)
4+
normalizy (1.0.0)
55
rails (>= 4.1, < 6)
66

77
GEM

README.md

+25-55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/wbotelhos/normalizy.svg)](https://travis-ci.org/wbotelhos/normalizy)
44
[![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)
56

67
Attribute normalizer for ActiveRecord.
78

@@ -112,8 +113,6 @@ Tue, 23 Oct 1984 00:00:00 EDT -04:00
112113
# Tue, 23 Oct 1984 11:59:59 EDT -04:00
113114
```
114115
115-
By default, `number` works with value before [Type Cast](#type-cast).
116-
117116
### Money
118117
119118
Transform a value to money format.
@@ -219,8 +218,6 @@ normalizy :amount, with: money: { cast: :to_f, type: :cents }
219218
# 4200.0
220219
```
221220

222-
By default, `money` works with value before [Type Cast](#type-cast).
223-
224221
### Number
225222

226223
```ruby
@@ -239,8 +236,6 @@ normalizy :age, with: number: { cast: :to_i }
239236
# 32
240237
```
241238

242-
By default, `number` works with value before [Type Cast](#type-cast).
243-
244239
### Percent
245240

246241
Transform a value to percent format.
@@ -346,8 +341,6 @@ normalizy :amount, with: percent: { cast: :to_f, type: :integer }
346341
# 4200.0
347342
```
348343

349-
By default, `percent` works with value before [Type Cast](#type-cast).
350-
351344
### Strip
352345

353346
Options:
@@ -392,33 +385,40 @@ normalizy :name, with: %i[squish titleize]
392385

393386
## Multiple Attributes
394387

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:
396389

397390
```ruby
398391
normalizy :email, :username, with: :downcase
399392
```
400393

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:
403397

404398
```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'
407404
```
408405

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)
410409

411410
## Default Filters
412411

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`:
414414

415415
```ruby
416416
Normalizy.configure do |config|
417417
config.default_filters = [:squish]
418418
end
419419
```
420420

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.
422422

423423
```ruby
424424
normalizy :name
@@ -580,37 +580,6 @@ Normalizy.configure do |config|
580580
end
581581
```
582582

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-
614583
## Alias
615584

616585
Sometimes you want to give a better name to your filter, just to keep the things semantic.
@@ -623,13 +592,6 @@ end
623592
```
624593

625594
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-
```
633595

634596
And now, the aliased filter will work fine:
635597

@@ -648,6 +610,14 @@ Normalizy.configure do |config|
648610
end
649611
```
650612

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+
651621
## RSpec
652622

653623
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 }) }
685655

686656
## Love it!
687657

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! (:

lib/normalizy/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Normalizy
4-
VERSION = '0.2.0'
4+
VERSION = '1.0.0'.freeze
55
end

0 commit comments

Comments
 (0)