normalizy 0.2.0 → 1.0.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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +25 -55
  4. data/lib/normalizy/config.rb +3 -7
  5. data/lib/normalizy/extensions.rb +45 -49
  6. data/lib/normalizy/rspec/matcher.rb +14 -2
  7. data/lib/normalizy/version.rb +1 -1
  8. data/lib/normalizy.rb +1 -1
  9. data/spec/normalizy/config/alias_spec.rb +14 -42
  10. data/spec/normalizy/extensions/filters/date_spec.rb +4 -14
  11. data/spec/normalizy/extensions/filters/money_spec.rb +9 -19
  12. data/spec/normalizy/extensions/filters/number_spec.rb +2 -6
  13. data/spec/normalizy/extensions/filters/percent_spec.rb +9 -19
  14. data/spec/normalizy/extensions/filters/strip_spec.rb +8 -8
  15. data/spec/normalizy/extensions/model_spec.rb +107 -0
  16. data/spec/normalizy/extensions/normalizy_rules_spec.rb +132 -180
  17. data/spec/normalizy/filters/date_spec.rb +9 -9
  18. data/spec/normalizy/filters/money_spec.rb +42 -37
  19. data/spec/normalizy/rspec/matcher/description_spec.rb +8 -7
  20. data/spec/normalizy/rspec/matcher/failure_message_spec.rb +25 -47
  21. data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +13 -15
  22. data/spec/normalizy/rspec/matcher/from_spec.rb +4 -3
  23. data/spec/normalizy/rspec/matcher/matches_spec.rb +117 -0
  24. data/spec/normalizy/rspec/matcher/to_spec.rb +4 -3
  25. data/spec/support/db/schema.rb +67 -9
  26. data/spec/support/filters/{blacklist_1.rb → blacklist.rb} +1 -1
  27. data/spec/support/filters/{blacklist_block.rb → block.rb} +1 -1
  28. data/spec/support/filters/info.rb +11 -0
  29. data/spec/support/models/alias.rb +7 -0
  30. data/spec/support/models/match.rb +9 -0
  31. data/spec/support/models/model.rb +28 -0
  32. data/spec/support/models/model_date.rb +7 -0
  33. data/spec/support/models/model_money.rb +10 -0
  34. data/spec/support/models/model_number.rb +5 -0
  35. data/spec/support/models/model_percent.rb +10 -0
  36. data/spec/support/models/model_strip.rb +8 -0
  37. data/spec/support/models/{clean.rb → rule.rb} +1 -1
  38. metadata +30 -20
  39. data/spec/normalizy/config/normalizy_raws_spec.rb +0 -9
  40. data/spec/normalizy/extensions/apply_normalizy_spec.rb +0 -171
  41. data/spec/normalizy/rspec/matcher/matchers_spec.rb +0 -97
  42. data/spec/normalizy/rspec/normalizy_spec.rb +0 -8
  43. data/spec/support/filters/blacklist_2.rb +0 -13
  44. data/spec/support/models/user.rb +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76436253c67c497cba120a39bd71667b34cad65f
4
- data.tar.gz: c5bb1d390b33e655ea7e5e9c865cf4bf8d903fc6
3
+ metadata.gz: d91babea23ed3c9c18a9aa9629da1139d1f12c55
4
+ data.tar.gz: 7ab05fe0149743bf72ea4c50b8095e029a11158d
5
5
  SHA512:
6
- metadata.gz: 488852bd2f5c2ce038533f7dcd984b537b5e27cd185cccafa560553cf7f50a13d8a7bac0e15670c6647fff376957b91a53463b966713f887ed5e566f68d32415
7
- data.tar.gz: 7205c44545020100f4663e2ca7e9d9af224dfda0e3bb6645acee7defb8798c76bec57582678a25670a5f4c07d2f6d07fed151d089e81f72e154ca6fd20afb806
6
+ metadata.gz: cb83724af9af4b195de644457cf7928ea71b218a77ed15d2ae5f822fffa5065531f8cb5a6d65b28a38048d1d238a43bd89894639634729c2dbff19d01f9c93a7
7
+ data.tar.gz: 5852ecb8103d2fa5849a61919216c297a493d500d7f41d00059c20abb62996c8c398788ccf1ce637af299b0878775846a970e346471a4c04adcf6b215ed104c4
data/CHANGELOG.md CHANGED
@@ -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
+
1
13
  ## v0.2.0
2
14
 
3
15
  - Changes:
data/README.md CHANGED
@@ -2,6 +2,7 @@
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,25 +385,32 @@ 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|
@@ -418,7 +418,7 @@ Normalizy.configure do |config|
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,13 +592,6 @@ end
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! (:
@@ -5,19 +5,16 @@ require 'normalizy/filters'
5
5
  module Normalizy
6
6
  class Config
7
7
  attr_accessor :default_filters
8
- attr_reader :filters, :normalizy_aliases, :normalizy_raws
8
+ attr_reader :filters, :normalizy_aliases
9
9
 
10
- def add(name, value, raw: false)
10
+ def add(name, value)
11
11
  @filters[name] = value
12
12
 
13
- @normalizy_raws << name if raw
14
-
15
13
  self
16
14
  end
17
15
 
18
- def alias(name, to, raw: false)
16
+ def alias(name, to)
19
17
  @normalizy_aliases[name] = to
20
- @normalizy_raws << name if raw
21
18
 
22
19
  self
23
20
  end
@@ -25,7 +22,6 @@ module Normalizy
25
22
  def initialize
26
23
  @default_filters = {}
27
24
  @normalizy_aliases = {}
28
- @normalizy_raws = %i[date money number percent]
29
25
 
30
26
  @filters = {
31
27
  date: Normalizy::Filters::Date,
@@ -5,27 +5,25 @@ module Normalizy
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- before_validation :apply_normalizy, if: -> {
9
- self.class.respond_to? :normalizy
10
- }
11
-
12
8
  private
13
9
 
14
- def apply_normalizy
15
- (self.class.normalizy_rules || {}).each do |attribute, rules|
16
- rules.each { |rule| normalizy! rule.merge(attribute: attribute) }
17
- end
10
+ def extract_filter(rule, rule_options, attribute, filters: Normalizy.config.filters)
11
+ options = rule_options.merge(attribute: attribute, object: self)
12
+
13
+ return [filters[rule] || rule, options] unless rule.is_a?(Hash)
14
+
15
+ filter = filters[rule.keys.first] || rule
16
+ options = (rule.values.first || {}).merge(options)
17
+
18
+ [filter, options]
18
19
  end
19
20
 
20
- def extract_filter(rule, filters: Normalizy.config.filters)
21
+ def extract_rule(rule)
21
22
  if rule.is_a?(Hash)
22
- result = filters[rule.keys.first] || rule.keys.first
23
- options = rule.values.first
23
+ [rule.keys.first, rule.values.first]
24
24
  else
25
- result = filters[rule]
25
+ [rule, {}]
26
26
  end
27
-
28
- [result || rule, options || {}]
29
27
  end
30
28
 
31
29
  def extract_value(value, filter, options, block)
@@ -36,7 +34,11 @@ module Normalizy
36
34
  filter.call value, &block
37
35
  end
38
36
  elsif respond_to?(filter)
39
- send filter, value, options, &block
37
+ if method(filter).arity == -2
38
+ send filter, value, options, &block
39
+ else
40
+ send filter, value, &block
41
+ end
40
42
  elsif value.respond_to?(filter)
41
43
  value.send filter, &block
42
44
  else
@@ -44,49 +46,27 @@ module Normalizy
44
46
  end
45
47
  end
46
48
 
47
- def normalizy!(attribute:, rules:, options:, block:)
49
+ def normalizy!(attribute:, block:, options:, rules:, value:)
50
+ rules ||= Normalizy.config.default_filters
51
+
48
52
  return if rules.blank? && block.blank?
49
53
 
50
- aliases = Normalizy.config.normalizy_aliases
51
- value = nil
54
+ result = value
52
55
 
53
56
  [rules].flatten.compact.each do |rule|
54
- result_rules = [aliases.key?(rule) ? aliases[rule] : rule]
55
-
56
- result_rules.flatten.compact.each do |result_rule|
57
- filter, filter_options = extract_filter(result_rule)
58
-
59
- if filter.respond_to?(:name)
60
- rule_name = filter.name.tableize.split('/').last.singularize.to_sym
61
- end
57
+ rule_name, rule_options = extract_rule(rule)
62
58
 
63
- original = original_value(attribute, rule_name, options)
64
- full_options = filter_options.merge(attribute: attribute, object: self)
65
- value = extract_value(original, filter, full_options, block)
59
+ unalias_for(rule_name).each do |rule|
60
+ filter, filter_options = extract_filter(rule, rule_options, attribute)
61
+ result = extract_value(result, filter, filter_options, block)
66
62
  end
67
63
  end
68
64
 
69
- write attribute, value
70
- end
71
-
72
- def original_value(attribute, rule, options)
73
- if raw? attribute, rule, options
74
- send "#{attribute}_before_type_cast"
75
- else
76
- send attribute
77
- end
65
+ result
78
66
  end
79
67
 
80
- def raw?(attribute, rule, options)
81
- return false unless respond_to?("#{attribute}_before_type_cast")
82
-
83
- options[:raw] || Normalizy.config.normalizy_raws.include?(rule)
84
- end
85
-
86
- def write(attribute, value)
87
- write_attribute attribute, value
88
- rescue ActiveModel::MissingAttributeError
89
- send "#{attribute}=", value
68
+ def unalias_for(rule, aliases: Normalizy.config.normalizy_aliases)
69
+ [aliases.key?(rule) ? aliases[rule] : rule].flatten.compact
90
70
  end
91
71
  end
92
72
 
@@ -95,7 +75,7 @@ module Normalizy
95
75
 
96
76
  def normalizy(*args, &block)
97
77
  options = args.extract_options!
98
- rules = options[:with] || Normalizy.config.default_filters
78
+ rules = options[:with]
99
79
 
100
80
  self.normalizy_rules ||= {}
101
81
 
@@ -103,6 +83,22 @@ module Normalizy
103
83
  normalizy_rules[field] ||= []
104
84
  normalizy_rules[field] << { block: block, options: options.except(:with), rules: rules }
105
85
  end
86
+
87
+ prepend Module.new {
88
+ args.each do |attribute|
89
+ define_method :"#{attribute}=" do |value|
90
+ result = normalizy!(
91
+ attribute: attribute,
92
+ block: block,
93
+ options: options.except(:with),
94
+ rules: rules,
95
+ value: value
96
+ )
97
+
98
+ super result
99
+ end
100
+ end
101
+ }
106
102
  end
107
103
  end
108
104
  end
@@ -43,6 +43,12 @@ 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
49
+
50
+ return false if options.blank?
51
+
46
52
  options.each do |option|
47
53
  rules = option[:rules]
48
54
 
@@ -52,9 +58,9 @@ module Normalizy
52
58
 
53
59
  false
54
60
  else
55
- @subject.send "#{@attribute}=", @from
61
+ @subject.send :"#{@attribute}=", @from
56
62
 
57
- @subject.send(@attribute) == @to
63
+ @subject[@attribute] == @to
58
64
  end
59
65
  end
60
66
 
@@ -80,6 +86,12 @@ module Normalizy
80
86
  value.is_a?(String) ? %("#{value}") : value
81
87
  end
82
88
 
89
+ def default_rules
90
+ [Normalizy.config.default_filters].flatten.compact.map do |rule|
91
+ { rules: rule }
92
+ end
93
+ end
94
+
83
95
  def from_value
84
96
  @from.nil? ? :nil : %("#{@from}")
85
97
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Normalizy
4
- VERSION = '0.2.0'
4
+ VERSION = '1.0.0'.freeze
5
5
  end
data/lib/normalizy.rb CHANGED
@@ -17,4 +17,4 @@ require 'normalizy/config'
17
17
  require 'normalizy/extensions'
18
18
  require 'normalizy/rspec/matcher'
19
19
 
20
- ActiveRecord::Base.send :include, Normalizy::Extension
20
+ ActiveRecord::Base.include Normalizy::Extension
@@ -3,55 +3,27 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Normalizy::Config, '#alias' do
6
- context 'with no raw type' do
7
- let!(:object) { User.new name: 'Washington Botelho' }
8
-
9
- before do
10
- object.class.normalizy_rules = {}
11
-
12
- Normalizy.configure do |config|
13
- config.alias :email, :downcase
14
- end
6
+ it 'accepts alias' do
7
+ Normalizy.configure do |config|
8
+ config.alias :email, :downcase
15
9
  end
16
10
 
17
- it 'alias one filter to others' do
18
- object.class.normalizy :name, with: :email
19
-
20
- object.save
21
-
22
- expect(object.name).to eq 'washington botelho'
23
- end
11
+ expect(Alias.create(email: 'Botelho').email).to eq 'botelho'
24
12
  end
25
13
 
26
- context 'with raw type' do
27
- before { User.normalizy_rules = {} }
28
-
29
- context 'configured on setup' do
30
- before do
31
- Normalizy.configure do |config|
32
- config.alias :age, :number, raw: true
33
- end
34
- end
35
-
36
- it 'alias one filter to others' do
37
- User.normalizy :age, with: :age
38
-
39
- expect(User.create(age: '= 42').age).to eq 42
40
- end
14
+ it 'accepts alias with options' do
15
+ Normalizy.configure do |config|
16
+ config.alias :with_arg, strip: { side: :left }
41
17
  end
42
18
 
43
- context 'configured on normalizy' do
44
- before do
45
- Normalizy.configure do |config|
46
- config.alias :age, :number
47
- end
48
- end
49
-
50
- it 'alias one filter to others' do
51
- User.normalizy :age, with: :age, raw: true
19
+ expect(Alias.create(with_arg_field: ' trim').with_arg_field).to eq 'trim'
20
+ end
52
21
 
53
- expect(User.create(age: '= 42').age).to eq 42
54
- end
22
+ it 'accepts late options' do
23
+ Normalizy.configure do |config|
24
+ config.alias :with_inline_arg, :strip
55
25
  end
26
+
27
+ expect(Alias.create(with_inline_arg_field: ' trim').with_inline_arg_field).to eq 'trim'
56
28
  end
57
29
  end
@@ -2,33 +2,23 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe User, 'filters:date' do
6
- before do
7
- described_class.normalizy_rules = {}
8
- end
9
-
5
+ RSpec.describe ModelDate, 'filters:date' do
10
6
  specify do
11
- described_class.normalizy :birthday, with: :date
12
-
13
7
  expected = Time.new(1984, 10, 23, 0, 0, 0, 0)
14
8
 
15
- expect(described_class.create(birthday: '1984-10-23').birthday).to eq expected
9
+ expect(described_class.create(date: '1984-10-23').date).to eq expected
16
10
  end
17
11
 
18
12
  specify do
19
- described_class.normalizy :birthday, with: { date: { format: '%y/%m/%d' } }
20
-
21
13
  expected = Time.new(1984, 10, 23, 0, 0, 0, 0)
22
14
 
23
- expect(described_class.create(birthday: '84/10/23').birthday).to eq expected
15
+ expect(described_class.create(date_format: '84/10/23').date_format).to eq expected
24
16
  end
25
17
 
26
18
  specify do
27
- described_class.normalizy :birthday, with: { date: { time_zone: 'Brasilia' } }
28
-
29
19
  hours = offset_in_hours('America/Sao_Paulo')
30
20
  expected = Time.new(1984, 10, 23, 0, 0, 0, 0) + (hours.hours * -1)
31
21
 
32
- expect(described_class.create(birthday: '1984-10-23').birthday).to eq expected
22
+ expect(described_class.create(date_time_zone: '1984-10-23').date_time_zone).to eq expected
33
23
  end
34
24
  end
@@ -2,39 +2,29 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe User, 'filters:money' do
6
- before do
7
- described_class.normalizy_rules = {}
5
+ RSpec.describe ModelMoney, 'filters:money' do
6
+ specify do
7
+ expect(described_class.create(text: '$ 42.00').text).to eq '42.00'
8
+ expect(described_class.create(text: '$ 42.10').text).to eq '42.10'
8
9
  end
9
10
 
10
11
  specify do
11
- described_class.normalizy :amount_text, with: :money
12
-
13
- expect(described_class.create(amount_text: '$ 42.00').amount_text).to eq '42.00'
14
- expect(described_class.create(amount_text: '$ 42.10').amount_text).to eq '42.10'
12
+ expect(described_class.create(cents_type: '$ 42.33').cents_type).to eq '4233'
15
13
  end
16
14
 
17
15
  specify do
18
- described_class.normalizy :amount_cents, with: { money: { type: :cents } }
19
-
20
- expect(described_class.create(amount_cents: '$ 42.33').amount_cents).to be 4233
16
+ expect(described_class.create(cast_to_i: '$ 42.00').cast_to_i).to be 42
21
17
  end
22
18
 
23
19
  specify do
24
- described_class.normalizy :amount_cents, with: { money: { cast: :to_i } }
25
-
26
- expect(described_class.create(amount_cents: '$ 42.00').amount_cents).to be 42
20
+ expect(described_class.create(cast_to_d: '$ 1.23').cast_to_d).to eq 1.23
27
21
  end
28
22
 
29
23
  specify do
30
- described_class.normalizy :amount, with: { money: { cast: :to_d } }
31
-
32
- expect(described_class.create(amount: '$ 1.23').amount).to eq 1.23.to_d
24
+ expect(described_class.create(cents_type_and_cast_to_f: '$ 42.00').cents_type_and_cast_to_f).to eq 4200.0
33
25
  end
34
26
 
35
27
  specify do
36
- described_class.normalizy :amount, with: { money: { cast: :to_f, type: :cents } }
37
-
38
- expect(described_class.create(amount: '$ 42.00').amount).to eq 4200.0.to_f
28
+ expect(described_class.create(cents_type_and_cast_to_i: '$ 42.00').cents_type_and_cast_to_i).to eq 4200
39
29
  end
40
30
  end
@@ -2,12 +2,8 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe User, 'filters:number' do
6
- before { described_class.normalizy_rules = {} }
7
-
5
+ RSpec.describe ModelNumber, 'filters:number' do
8
6
  specify do
9
- described_class.normalizy :name, with: :number
10
-
11
- expect(described_class.create(name: 'Washington 32').name).to eq '32'
7
+ expect(described_class.create(number: 'Botelho 32').number).to eq '32'
12
8
  end
13
9
  end
@@ -2,39 +2,29 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe User, 'filters:percent' do
6
- before do
7
- described_class.normalizy_rules = {}
5
+ RSpec.describe ModelPercent, 'filters:percent' do
6
+ specify do
7
+ expect(described_class.create(text: '42.00 %').text).to eq '42.00'
8
+ expect(described_class.create(text: '42.10 %').text).to eq '42.10'
8
9
  end
9
10
 
10
11
  specify do
11
- described_class.normalizy :amount_text, with: :percent
12
-
13
- expect(described_class.create(amount_text: '42.00 %').amount_text).to eq '42.00'
14
- expect(described_class.create(amount_text: '42.10 %').amount_text).to eq '42.10'
12
+ expect(described_class.create(cents_type: '42.33 %').cents_type).to eq '4233'
15
13
  end
16
14
 
17
15
  specify do
18
- described_class.normalizy :amount_cents, with: { percent: { type: :cents } }
19
-
20
- expect(described_class.create(amount_cents: '42.33 %').amount_cents).to be 4233
16
+ expect(described_class.create(cast_to_i: '42.00 %').cast_to_i).to be 42
21
17
  end
22
18
 
23
19
  specify do
24
- described_class.normalizy :amount_cents, with: { percent: { cast: :to_i } }
25
-
26
- expect(described_class.create(amount_cents: '42.00 %').amount_cents).to be 42
20
+ expect(described_class.create(cast_to_d: '1.23 %').cast_to_d).to eq 1.23
27
21
  end
28
22
 
29
23
  specify do
30
- described_class.normalizy :amount, with: { percent: { cast: :to_d } }
31
-
32
- expect(described_class.create(amount: '1.23 %').amount).to eq 1.23.to_d
24
+ expect(described_class.create(cents_type_and_cast_to_f: '42.00 %').cents_type_and_cast_to_f).to eq 4200.0
33
25
  end
34
26
 
35
27
  specify do
36
- described_class.normalizy :amount, with: { percent: { cast: :to_f, type: :cents } }
37
-
38
- expect(described_class.create(amount: '42.00 %').amount).to eq 4200.0.to_f
28
+ expect(described_class.create(cents_type_and_cast_to_i: '42.00 %').cents_type_and_cast_to_i).to eq 4200
39
29
  end
40
30
  end
@@ -2,20 +2,20 @@
2
2
 
3
3
  require 'rails_helper'
4
4
 
5
- RSpec.describe User, 'filters:strip' do
6
- before do
7
- described_class.normalizy_rules = {}
5
+ RSpec.describe ModelStrip, 'filters:strip' do
6
+ specify do
7
+ expect(described_class.create(strip: ' Botelho ').strip).to eq 'Botelho'
8
8
  end
9
9
 
10
10
  specify do
11
- described_class.normalizy :name, with: :strip
12
-
13
- expect(described_class.create(name: ' Washington ').name).to eq 'Washington'
11
+ expect(described_class.create(strip_side_left: ' Botelho ').strip_side_left).to eq 'Botelho '
14
12
  end
15
13
 
16
14
  specify do
17
- described_class.normalizy :name, with: { strip: { side: :left } }
15
+ expect(described_class.create(strip_side_right: ' Botelho ').strip_side_right).to eq ' Botelho'
16
+ end
18
17
 
19
- expect(described_class.create(name: ' Washington ').name).to eq 'Washington '
18
+ specify do
19
+ expect(described_class.create(strip_side_both: ' Botelho ').strip_side_both).to eq 'Botelho'
20
20
  end
21
21
  end