normalize_attributes 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -1
- data/.travis.yml +15 -8
- data/Gemfile.lock +84 -0
- data/README.md +10 -8
- data/Rakefile +9 -4
- data/lib/normalize_attributes.rb +7 -4
- data/lib/normalize_attributes/active_record.rb +47 -35
- data/lib/normalize_attributes/version.rb +1 -1
- data/normalize_attributes.gemspec +8 -5
- data/test/normalize_attributes_test.rb +112 -0
- data/{spec → test}/schema.rb +0 -0
- data/{spec → test}/support/token.rb +0 -0
- data/{spec → test}/support/user.rb +0 -0
- data/test/test_helper.rb +16 -0
- metadata +47 -19
- data/.rspec +0 -1
- data/spec/normalize_attributes_spec.rb +0 -121
- data/spec/spec_helper.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5a24b07529124afe4d9fda460e8f10e3eabfe9ac824fe56174502d27d19780a3
|
4
|
+
data.tar.gz: 0aff6b7f98fb50480d3d933d3623ac39bf6e50b772b77baf32a78cb589e85742
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 570f5a329e89d4e82241abbf1310e05c17d2650959ad206fa099fa120431dd18fcc8304e50b652329b21a9ca7884713abf0115f5272c207cf929ec3fe39fe863
|
7
|
+
data.tar.gz: 25593e6b295b4662443f0ad150d3a90fa41b7d5eac10c4fdb63feec5e50246c8c586d72d025a9847184c35bd2cc475a80696a5cca5b5541c83b542c74c7713ff
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
language: ruby
|
2
2
|
sudo: false
|
3
|
-
script: bundle exec rspec
|
4
3
|
cache: bundler
|
4
|
+
notifications:
|
5
|
+
email: false
|
5
6
|
rvm:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
- 2.5.2
|
8
|
+
before_install:
|
9
|
+
- gem install bundler
|
10
|
+
before_script:
|
11
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
12
|
+
> ./cc-test-reporter
|
13
|
+
- chmod +x ./cc-test-reporter
|
14
|
+
- "./cc-test-reporter before-build"
|
15
|
+
after_script:
|
16
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
17
|
+
env:
|
18
|
+
global:
|
19
|
+
secure: U80HNhPVE+IUJaxOPxMJc0CqlQSsl23EOOsqcnuXUjrlcjvfeCnQU58rxFOUCBeKxmno4tatfZXdwBa+FvQ3PcnOXXnesMfCR0UAPdGmaA6ScSMyL+KijC1wGI0N80ccBZRaBA4quR02XTMiJt1saTrBT1cuOCsWs2gjOB4EwoA=
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
normalize_attributes (0.3.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (5.2.2)
|
11
|
+
actionview (= 5.2.2)
|
12
|
+
activesupport (= 5.2.2)
|
13
|
+
rack (~> 2.0)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
+
actionview (5.2.2)
|
18
|
+
activesupport (= 5.2.2)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
+
activemodel (5.2.2)
|
24
|
+
activesupport (= 5.2.2)
|
25
|
+
activerecord (5.2.2)
|
26
|
+
activemodel (= 5.2.2)
|
27
|
+
activesupport (= 5.2.2)
|
28
|
+
arel (>= 9.0)
|
29
|
+
activesupport (5.2.2)
|
30
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
31
|
+
i18n (>= 0.7, < 2)
|
32
|
+
minitest (~> 5.1)
|
33
|
+
tzinfo (~> 1.1)
|
34
|
+
arel (9.0.0)
|
35
|
+
builder (3.2.3)
|
36
|
+
concurrent-ruby (1.1.4)
|
37
|
+
crass (1.0.4)
|
38
|
+
docile (1.1.5)
|
39
|
+
erubi (1.8.0)
|
40
|
+
i18n (1.3.0)
|
41
|
+
concurrent-ruby (~> 1.0)
|
42
|
+
json (2.1.0)
|
43
|
+
loofah (2.2.3)
|
44
|
+
crass (~> 1.0.2)
|
45
|
+
nokogiri (>= 1.5.9)
|
46
|
+
mini_portile2 (2.4.0)
|
47
|
+
minitest (5.11.3)
|
48
|
+
minitest-utils (0.4.4)
|
49
|
+
minitest
|
50
|
+
nokogiri (1.9.1)
|
51
|
+
mini_portile2 (~> 2.4.0)
|
52
|
+
rack (2.0.6)
|
53
|
+
rack-test (1.1.0)
|
54
|
+
rack (>= 1.0, < 3)
|
55
|
+
rails-dom-testing (2.0.3)
|
56
|
+
activesupport (>= 4.2.0)
|
57
|
+
nokogiri (>= 1.6)
|
58
|
+
rails-html-sanitizer (1.0.4)
|
59
|
+
loofah (~> 2.2, >= 2.2.2)
|
60
|
+
rake (12.3.2)
|
61
|
+
simplecov (0.13.0)
|
62
|
+
docile (~> 1.1.0)
|
63
|
+
json (>= 1.8, < 3)
|
64
|
+
simplecov-html (~> 0.10.0)
|
65
|
+
simplecov-html (0.10.2)
|
66
|
+
sqlite3 (1.3.13)
|
67
|
+
thread_safe (0.3.6)
|
68
|
+
tzinfo (1.2.5)
|
69
|
+
thread_safe (~> 0.1)
|
70
|
+
|
71
|
+
PLATFORMS
|
72
|
+
ruby
|
73
|
+
|
74
|
+
DEPENDENCIES
|
75
|
+
actionpack
|
76
|
+
bundler
|
77
|
+
minitest-utils
|
78
|
+
normalize_attributes!
|
79
|
+
rake
|
80
|
+
simplecov
|
81
|
+
sqlite3
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
1.17.3
|
data/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# Normalize Attributes
|
2
2
|
|
3
|
-
[![
|
3
|
+
[![Travis-CI](https://travis-ci.org/fnando/normalize_attributes.svg)](https://travis-ci.org/fnando/normalize_attributes)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/fnando/normalize_attributes/badges/gpa.svg)](https://codeclimate.com/github/fnando/normalize_attributes)
|
5
|
-
[![Test Coverage](https://codeclimate.com/github/fnando/normalize_attributes/badges/coverage.svg)](https://codeclimate.com/github/fnando/normalize_attributes)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/fnando/normalize_attributes/badges/coverage.svg)](https://codeclimate.com/github/fnando/normalize_attributes/coverage)
|
6
|
+
[![Gem](https://img.shields.io/gem/v/normalize_attributes.svg)](https://rubygems.org/gems/normalize_attributes)
|
7
|
+
[![Gem](https://img.shields.io/gem/dt/normalize_attributes.svg)](https://rubygems.org/gems/normalize_attributes)
|
6
8
|
|
7
|
-
Sometimes you want to normalize data before saving it to the database like
|
9
|
+
Sometimes you want to normalize data before saving it to the database like downcasing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
|
8
10
|
|
9
11
|
## Usage
|
10
12
|
|
@@ -16,7 +18,7 @@ Then on your model:
|
|
16
18
|
|
17
19
|
```ruby
|
18
20
|
class User < ActiveRecord::Base
|
19
|
-
normalize :email, :
|
21
|
+
normalize :email, with: :downcase
|
20
22
|
end
|
21
23
|
```
|
22
24
|
|
@@ -25,7 +27,7 @@ The example above will normalize your `:email` attribute on the `before_save` ca
|
|
25
27
|
You can specify multiple attributes
|
26
28
|
|
27
29
|
```ruby
|
28
|
-
normalize :email, :username, :
|
30
|
+
normalize :email, :username, with: :downcase
|
29
31
|
```
|
30
32
|
|
31
33
|
You can use a block
|
@@ -39,7 +41,7 @@ end
|
|
39
41
|
You can combine both
|
40
42
|
|
41
43
|
```ruby
|
42
|
-
normalize :name, :
|
44
|
+
normalize :name, with: :downcase do |value|
|
43
45
|
value.squish
|
44
46
|
end
|
45
47
|
```
|
@@ -66,13 +68,13 @@ dates and numbers.
|
|
66
68
|
|
67
69
|
```ruby
|
68
70
|
class Product
|
69
|
-
normalize(:price, :
|
71
|
+
normalize(:price, raw: true) {|v| Money.new(v).to_f}
|
70
72
|
end
|
71
73
|
```
|
72
74
|
|
73
75
|
## Maintainer
|
74
76
|
|
75
|
-
* Nando Vieira (http://nandovieira.com
|
77
|
+
* Nando Vieira (http://nandovieira.com)
|
76
78
|
|
77
79
|
## License:
|
78
80
|
|
data/Rakefile
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
require "bundler"
|
2
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
7
|
+
t.warning = false
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :test
|
data/lib/normalize_attributes.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require "active_record"
|
2
|
-
require "active_support/core_ext/string/filters"
|
3
|
-
require "normalize_attributes/version"
|
4
|
-
require "normalize_attributes/active_record"
|
5
2
|
|
6
|
-
|
3
|
+
ActiveSupport.on_load(:active_record) do
|
4
|
+
require "active_support/core_ext/string/filters"
|
5
|
+
require "normalize_attributes/version"
|
6
|
+
require "normalize_attributes/active_record"
|
7
|
+
|
8
|
+
ActiveRecord::Base.send :include, NormalizeAttributes::ActiveRecord
|
9
|
+
end
|
@@ -1,4 +1,48 @@
|
|
1
1
|
module NormalizeAttributes
|
2
|
+
def self.retrieve_value(record, attribute, raw)
|
3
|
+
before_type_cast_method = "#{attribute}_before_type_cast"
|
4
|
+
|
5
|
+
if raw && record.respond_to?(before_type_cast_method)
|
6
|
+
record.send(before_type_cast_method)
|
7
|
+
else
|
8
|
+
record.send(attribute)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.retrieve_normalizers(normalizers, value)
|
13
|
+
return normalizers unless normalizers.empty?
|
14
|
+
|
15
|
+
case value
|
16
|
+
when String
|
17
|
+
[:squish]
|
18
|
+
when Array
|
19
|
+
[:compact]
|
20
|
+
else
|
21
|
+
[]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.apply_normalizers(record, attribute, normalizers, options)
|
26
|
+
value = NormalizeAttributes.retrieve_value(record, attribute, options[:raw])
|
27
|
+
normalizers = NormalizeAttributes.retrieve_normalizers(normalizers, value)
|
28
|
+
|
29
|
+
normalizers.each do |normalizer|
|
30
|
+
if normalizer.respond_to?(:call)
|
31
|
+
value = normalizer.call(value)
|
32
|
+
elsif value.respond_to?(normalizer)
|
33
|
+
value = value.send(normalizer)
|
34
|
+
elsif record.respond_to?(normalizer)
|
35
|
+
value = record.send(normalizer, value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
begin
|
40
|
+
record.write_attribute(attribute, value)
|
41
|
+
rescue ActiveModel::MissingAttributeError
|
42
|
+
record.public_send("#{attribute}=", value)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
2
46
|
module ActiveRecord
|
3
47
|
def self.included(base)
|
4
48
|
base.instance_eval do
|
@@ -35,48 +79,16 @@ module NormalizeAttributes
|
|
35
79
|
|
36
80
|
module InstanceMethods
|
37
81
|
private
|
82
|
+
|
38
83
|
def normalize_attributes
|
39
84
|
return unless self.class.normalize_options
|
40
85
|
|
41
|
-
self.class.normalize_options.each do |
|
86
|
+
self.class.normalize_options.each do |attribute, items|
|
42
87
|
items.each do |item|
|
43
|
-
apply_normalizers
|
88
|
+
NormalizeAttributes.apply_normalizers(self, attribute, *item.dup)
|
44
89
|
end
|
45
90
|
end
|
46
91
|
end
|
47
|
-
|
48
|
-
def apply_normalizers(name, normalizers, options)
|
49
|
-
if options[:raw] && respond_to?("#{name}_before_type_cast")
|
50
|
-
value = send("#{name}_before_type_cast")
|
51
|
-
else
|
52
|
-
value = send(name)
|
53
|
-
end
|
54
|
-
|
55
|
-
if normalizers.empty?
|
56
|
-
case value
|
57
|
-
when String
|
58
|
-
normalizers << :squish
|
59
|
-
when Array
|
60
|
-
normalizers << :compact
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
normalizers.each do |normalizer|
|
65
|
-
if normalizer.respond_to?(:call)
|
66
|
-
value = normalizer.call(value)
|
67
|
-
elsif value.respond_to?(normalizer)
|
68
|
-
value = value.send(normalizer)
|
69
|
-
elsif respond_to?(normalizer)
|
70
|
-
value = send(normalizer, value)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
begin
|
75
|
-
write_attribute name, value
|
76
|
-
rescue ActiveModel::MissingAttributeError
|
77
|
-
send :"#{name}=", value
|
78
|
-
end
|
79
|
-
end
|
80
92
|
end
|
81
93
|
end
|
82
94
|
end
|
@@ -8,16 +8,19 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.email = ["fnando.vieira@gmail.com"]
|
9
9
|
s.homepage = "http://rubygems.org/gems/normalize_attributes"
|
10
10
|
s.summary = "Normalize ActiveRecord attributes"
|
11
|
-
s.description =
|
11
|
+
s.description = s.summary
|
12
|
+
s.license = "MIT"
|
12
13
|
|
13
14
|
s.files = `git ls-files`.split("\n")
|
14
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) }
|
16
17
|
s.require_paths = ["lib"]
|
17
18
|
|
18
19
|
s.add_dependency "activerecord"
|
19
|
-
s.add_development_dependency "
|
20
|
-
s.add_development_dependency "
|
20
|
+
s.add_development_dependency "bundler"
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency "minitest-utils"
|
23
|
+
s.add_development_dependency "sqlite3"
|
21
24
|
s.add_development_dependency "actionpack"
|
22
|
-
s.add_development_dependency "
|
25
|
+
s.add_development_dependency "simplecov"
|
23
26
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class NormalizeAttributesTest < Minitest::Test
|
4
|
+
setup do
|
5
|
+
User.normalize_options = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
test "respond to aliases" do
|
9
|
+
assert User.respond_to?(:normalize)
|
10
|
+
assert User.respond_to?(:normalize_attr)
|
11
|
+
assert User.respond_to?(:normalize_attrs)
|
12
|
+
assert User.respond_to?(:normalize_attribute)
|
13
|
+
assert User.respond_to?(:normalize_attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
test "apply single normalization method" do
|
17
|
+
User.normalize :email, with: :downcase
|
18
|
+
user = User.create(email: "JOHN@DOE.COM")
|
19
|
+
|
20
|
+
assert_equal "john@doe.com", user.email
|
21
|
+
end
|
22
|
+
|
23
|
+
test "apply multiple normalization methods" do
|
24
|
+
User.normalize :email, with: %i[downcase reverse]
|
25
|
+
user = User.create(email: "JOHN@DOE.COM")
|
26
|
+
|
27
|
+
assert_equal "moc.eod@nhoj", user.email
|
28
|
+
end
|
29
|
+
|
30
|
+
test "apply proc" do
|
31
|
+
User.normalize(:email, &:downcase)
|
32
|
+
user = User.create(email: "JOHN@DOE.COM")
|
33
|
+
|
34
|
+
assert_equal "john@doe.com", user.email
|
35
|
+
end
|
36
|
+
|
37
|
+
test "apply instance method" do
|
38
|
+
User.normalize(:username, with: :normalize_username)
|
39
|
+
user = User.create(username: "JOHNDOE")
|
40
|
+
|
41
|
+
assert_equal "johndoe", user.username
|
42
|
+
end
|
43
|
+
|
44
|
+
test "use value before type casting" do
|
45
|
+
User.normalize(:age, raw: true) do |v|
|
46
|
+
v.to_f * 10
|
47
|
+
end
|
48
|
+
|
49
|
+
user = User.create(age: "1.2")
|
50
|
+
assert_equal 12, user.age
|
51
|
+
end
|
52
|
+
|
53
|
+
test "combine both method names and procs as normalization methods" do
|
54
|
+
User.normalize(:email, with: :downcase) {|v| v.reverse }
|
55
|
+
user = User.create(email: "JOHN@DOE.COM")
|
56
|
+
|
57
|
+
assert_equal "moc.eod@nhoj", user.email
|
58
|
+
end
|
59
|
+
|
60
|
+
test "normalize multiple attributes" do
|
61
|
+
User.normalize :email, :username, with: :downcase
|
62
|
+
user = User.create(email: "JOHN@DOE.COM", username: "JOHN")
|
63
|
+
|
64
|
+
assert_equal "john@doe.com", user.email
|
65
|
+
assert_equal "john", user.username
|
66
|
+
end
|
67
|
+
|
68
|
+
test "don't apply on associations" do
|
69
|
+
User.normalize :email, with: :downcase
|
70
|
+
user = User.create(email: "JOHN@DOE.COM", username: "JOHN")
|
71
|
+
|
72
|
+
user.tokens.create!
|
73
|
+
end
|
74
|
+
|
75
|
+
test "apply default filter on strings" do
|
76
|
+
User.normalize :email
|
77
|
+
user = User.create(email: " \n\t john@doe.com \t\t\n\r\n", username: "john")
|
78
|
+
|
79
|
+
assert_equal "john@doe.com", user.email
|
80
|
+
end
|
81
|
+
|
82
|
+
test "apply default filter on arrays" do
|
83
|
+
User.normalize :preferences
|
84
|
+
user = User.create(preferences: [nil, :games, :music])
|
85
|
+
|
86
|
+
assert_equal %i[games music], user.preferences
|
87
|
+
end
|
88
|
+
|
89
|
+
test "don't apply default filter on unknown objects" do
|
90
|
+
User.normalize :username
|
91
|
+
user = User.create(username: 100)
|
92
|
+
|
93
|
+
assert_equal "100", user.username
|
94
|
+
end
|
95
|
+
|
96
|
+
test "don't apply filter when object do not respond to normalizer" do
|
97
|
+
User.normalize :username, with: :missing
|
98
|
+
|
99
|
+
user = User.create(username: nil)
|
100
|
+
user.save
|
101
|
+
end
|
102
|
+
|
103
|
+
test "normalize attributes that are not backed by database columns" do
|
104
|
+
User.normalize :nickname, with: :downcase
|
105
|
+
|
106
|
+
user = User.new(username: "john@doe.com")
|
107
|
+
user.nickname = "JOHNNY D"
|
108
|
+
user.save
|
109
|
+
|
110
|
+
assert_equal "johnny d", user.nickname
|
111
|
+
end
|
112
|
+
end
|
data/{spec → test}/schema.rb
RENAMED
File without changes
|
File without changes
|
File without changes
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "normalize_attributes"
|
6
|
+
|
7
|
+
require "minitest/utils"
|
8
|
+
require "minitest/autorun"
|
9
|
+
|
10
|
+
ActiveRecord::Base.belongs_to_required_by_default = true
|
11
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
12
|
+
|
13
|
+
# Load database schema
|
14
|
+
load "schema.rb"
|
15
|
+
|
16
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: normalize_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,7 +39,35 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-utils
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
44
72
|
requirements:
|
45
73
|
- - ">="
|
@@ -67,7 +95,7 @@ dependencies:
|
|
67
95
|
- !ruby/object:Gem::Version
|
68
96
|
version: '0'
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
98
|
+
name: simplecov
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
72
100
|
requirements:
|
73
101
|
- - ">="
|
@@ -88,22 +116,23 @@ extensions: []
|
|
88
116
|
extra_rdoc_files: []
|
89
117
|
files:
|
90
118
|
- ".gitignore"
|
91
|
-
- ".rspec"
|
92
119
|
- ".travis.yml"
|
93
120
|
- Gemfile
|
121
|
+
- Gemfile.lock
|
94
122
|
- README.md
|
95
123
|
- Rakefile
|
96
124
|
- lib/normalize_attributes.rb
|
97
125
|
- lib/normalize_attributes/active_record.rb
|
98
126
|
- lib/normalize_attributes/version.rb
|
99
127
|
- normalize_attributes.gemspec
|
100
|
-
-
|
101
|
-
-
|
102
|
-
-
|
103
|
-
-
|
104
|
-
-
|
128
|
+
- test/normalize_attributes_test.rb
|
129
|
+
- test/schema.rb
|
130
|
+
- test/support/token.rb
|
131
|
+
- test/support/user.rb
|
132
|
+
- test/test_helper.rb
|
105
133
|
homepage: http://rubygems.org/gems/normalize_attributes
|
106
|
-
licenses:
|
134
|
+
licenses:
|
135
|
+
- MIT
|
107
136
|
metadata: {}
|
108
137
|
post_install_message:
|
109
138
|
rdoc_options: []
|
@@ -120,14 +149,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
149
|
- !ruby/object:Gem::Version
|
121
150
|
version: '0'
|
122
151
|
requirements: []
|
123
|
-
|
124
|
-
rubygems_version: 2.4.6
|
152
|
+
rubygems_version: 3.0.1
|
125
153
|
signing_key:
|
126
154
|
specification_version: 4
|
127
155
|
summary: Normalize ActiveRecord attributes
|
128
156
|
test_files:
|
129
|
-
-
|
130
|
-
-
|
131
|
-
-
|
132
|
-
-
|
133
|
-
-
|
157
|
+
- test/normalize_attributes_test.rb
|
158
|
+
- test/schema.rb
|
159
|
+
- test/support/token.rb
|
160
|
+
- test/support/user.rb
|
161
|
+
- test/test_helper.rb
|
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
@@ -1,121 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Normalize Attributes" do
|
4
|
-
before do
|
5
|
-
User.normalize_options = {}
|
6
|
-
end
|
7
|
-
|
8
|
-
it "should respond to aliases" do
|
9
|
-
expect(User).to respond_to(:normalize)
|
10
|
-
expect(User).to respond_to(:normalize_attr)
|
11
|
-
expect(User).to respond_to(:normalize_attrs)
|
12
|
-
expect(User).to respond_to(:normalize_attribute)
|
13
|
-
expect(User).to respond_to(:normalize_attributes)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should apply single normalization method" do
|
17
|
-
User.normalize :email, :with => :downcase
|
18
|
-
user = User.create(:email => "JOHN@DOE.COM")
|
19
|
-
|
20
|
-
expect(user.email).to eq("john@doe.com")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should apply multiple normalization methods" do
|
24
|
-
User.normalize :email, :with => [:downcase, :reverse]
|
25
|
-
user = User.create(:email => "JOHN@DOE.COM")
|
26
|
-
|
27
|
-
expect(user.email).to eq("moc.eod@nhoj")
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should apply proc" do
|
31
|
-
User.normalize(:email) {|v| v.downcase }
|
32
|
-
user = User.create(:email => "JOHN@DOE.COM")
|
33
|
-
|
34
|
-
expect(user.email).to eq("john@doe.com")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should apply instance method" do
|
38
|
-
User.normalize(:username, :with => :normalize_username)
|
39
|
-
user = User.create(:username => "JOHNDOE")
|
40
|
-
|
41
|
-
expect(user.username).to eq("johndoe")
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should use value before type casting" do
|
45
|
-
User.normalize(:age, :raw => true) do |v|
|
46
|
-
expect(v).to eq("1.2")
|
47
|
-
v.to_f * 10
|
48
|
-
end
|
49
|
-
|
50
|
-
user = User.create(:age => "1.2")
|
51
|
-
expect(user.age).to eq(12)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should apply normalizers to accessor" do
|
55
|
-
# User.normalize()
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should combine both method names and procs as normalization methods" do
|
59
|
-
User.normalize(:email, :with => :downcase) {|v| v.reverse }
|
60
|
-
user = User.create(:email => "JOHN@DOE.COM")
|
61
|
-
|
62
|
-
expect(user.email).to eq("moc.eod@nhoj")
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should normalize multiple attributes" do
|
66
|
-
User.normalize :email, :username, :with => :downcase
|
67
|
-
user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
|
68
|
-
|
69
|
-
expect(user.email).to eq("john@doe.com")
|
70
|
-
expect(user.username).to eq("john")
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should not apply on associations" do
|
74
|
-
User.normalize :email, :with => :downcase
|
75
|
-
user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
|
76
|
-
|
77
|
-
expect {
|
78
|
-
user.tokens.create!
|
79
|
-
}.to_not raise_error
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should apply default filter on strings" do
|
83
|
-
User.normalize :email
|
84
|
-
user = User.create(:email => " \n\t john@doe.com \t\t\n\r\n", :username => "john")
|
85
|
-
|
86
|
-
expect(user.email).to eq("john@doe.com")
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should apply default filter on arrays" do
|
90
|
-
User.normalize :preferences
|
91
|
-
user = User.create(:preferences => [nil, :games, :music])
|
92
|
-
expect(user.preferences).to eq([:games, :music])
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should not apply default filter on unknown objects" do
|
96
|
-
User.normalize :username
|
97
|
-
|
98
|
-
expect {
|
99
|
-
user = User.create(:username => 100)
|
100
|
-
expect(user.username).to eq("100")
|
101
|
-
}.not_to raise_error
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should not apply filter when object do not respond to normalizer" do
|
105
|
-
User.normalize :username, :with => :missing
|
106
|
-
|
107
|
-
expect {
|
108
|
-
user = User.create(:username => nil)
|
109
|
-
user.save
|
110
|
-
}.to_not raise_error
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should normalize attributes that are not backed by database columns" do
|
114
|
-
User.normalize :nickname, :with => :downcase
|
115
|
-
|
116
|
-
user = User.new(:username => "john@doe.com")
|
117
|
-
user.nickname = "JOHNNY D"
|
118
|
-
expect { user.save }.to_not raise_error
|
119
|
-
expect(user.nickname).to eq("johnny d")
|
120
|
-
end
|
121
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require "codeclimate-test-reporter"
|
2
|
-
CodeClimate::TestReporter.start
|
3
|
-
|
4
|
-
require "rspec"
|
5
|
-
require "normalize_attributes"
|
6
|
-
|
7
|
-
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
8
|
-
|
9
|
-
# Load database schema
|
10
|
-
load "schema.rb"
|
11
|
-
|
12
|
-
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|