data_cleansing 0.9.0 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +3 -25
- data/Rakefile +18 -14
- data/lib/data_cleansing/cleaners.rb +35 -21
- data/lib/data_cleansing/cleanse.rb +52 -73
- data/lib/data_cleansing/data_cleansing.rb +19 -1
- data/lib/data_cleansing/railtie.rb +0 -1
- data/lib/data_cleansing/version.rb +1 -1
- data/lib/data_cleansing.rb +6 -8
- data/test/active_record_test.rb +56 -53
- data/test/cleaners_test.rb +84 -73
- data/test/data_cleansing_test.rb +9 -0
- data/test/ruby_test.rb +48 -49
- data/test/test_db.sqlite3 +0 -0
- data/test/test_helper.rb +6 -10
- metadata +11 -11
data/test/ruby_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "test_helper"
|
2
2
|
|
3
3
|
# Non Cleansing base class
|
4
4
|
class RubyUserBase
|
@@ -11,10 +11,10 @@ class RubyUser < RubyUserBase
|
|
11
11
|
attr_accessor :first_name, :last_name, :address1, :address2
|
12
12
|
|
13
13
|
# Use a global cleaner
|
14
|
-
cleanse :first_name, :last_name, :
|
14
|
+
cleanse :first_name, :last_name, cleaner: :strip
|
15
15
|
|
16
16
|
# Define a once off cleaner
|
17
|
-
cleanse :address1, :address2, :
|
17
|
+
cleanse :address1, :address2, cleaner: proc { |string| "<< #{string.strip} >>" }
|
18
18
|
|
19
19
|
# Execute after cleanser
|
20
20
|
after_cleanse :name_check
|
@@ -31,7 +31,8 @@ end
|
|
31
31
|
|
32
32
|
class RubyUserChild < RubyUser
|
33
33
|
attr_accessor :gender
|
34
|
-
|
34
|
+
|
35
|
+
cleanse :gender, cleaner: proc { |gender| gender.to_s.strip.downcase }
|
35
36
|
end
|
36
37
|
|
37
38
|
class RubyUser2
|
@@ -40,20 +41,20 @@ class RubyUser2
|
|
40
41
|
attr_accessor :first_name, :last_name, :title, :address1, :address2, :gender
|
41
42
|
|
42
43
|
# Use a global cleaner
|
43
|
-
cleanse :first_name, :last_name, :
|
44
|
+
cleanse :first_name, :last_name, cleaner: :strip
|
44
45
|
|
45
46
|
# Define a once off cleaner
|
46
|
-
cleanse :address1, :address2, :
|
47
|
+
cleanse :address1, :address2, cleaner: proc { |string| string.strip }
|
47
48
|
|
48
49
|
# Use multiple cleaners, and a custom block
|
49
|
-
cleanse :title, :
|
50
|
+
cleanse :title, cleaner: [:strip, :upcase, proc { |string| "#{string}." unless string.end_with?(".") }]
|
50
51
|
|
51
52
|
# Change the cleansing rule based on the value of other attributes in that instance of user
|
52
53
|
# The 'title' is retrieved from the current instance of the user
|
53
|
-
cleanse :gender, :
|
54
|
+
cleanse :gender, cleaner: [
|
54
55
|
:strip,
|
55
56
|
:upcase,
|
56
|
-
|
57
|
+
proc do |gender|
|
57
58
|
if (gender == "UNKNOWN") && (title == "MR.")
|
58
59
|
"Male"
|
59
60
|
else
|
@@ -65,112 +66,110 @@ end
|
|
65
66
|
|
66
67
|
class RubyTest < Minitest::Test
|
67
68
|
describe "Ruby Models" do
|
68
|
-
|
69
|
-
it 'have globally registered cleaner' do
|
69
|
+
it "have globally registered cleaner" do
|
70
70
|
assert DataCleansing.cleaner(:strip)
|
71
71
|
end
|
72
72
|
|
73
|
-
it
|
74
|
-
assert_equal
|
75
|
-
assert_equal
|
76
|
-
assert_equal
|
77
|
-
assert_equal
|
73
|
+
it "Model.cleanse_attribute" do
|
74
|
+
assert_equal "male", RubyUserChild.cleanse_attribute(:gender, "\n Male \n"), RubyUserChild.send(:data_cleansing_attribute_cleaners)
|
75
|
+
assert_equal "joe", RubyUserChild.cleanse_attribute(:first_name, " joe "), RubyUserChild.send(:data_cleansing_attribute_cleaners)
|
76
|
+
assert_equal "black", RubyUserChild.cleanse_attribute(:last_name, "\n black\n"), RubyUserChild.send(:data_cleansing_attribute_cleaners)
|
77
|
+
assert_equal "<< 2632 Brown St >>", RubyUserChild.cleanse_attribute(:address1, "2632 Brown St \n"), RubyUserChild.send(:data_cleansing_attribute_cleaners)
|
78
|
+
assert_equal 3, RubyUserChild.cleanse_attribute(:first_name, 3), RubyUserChild.send(:data_cleansing_attribute_cleaners)
|
78
79
|
end
|
79
80
|
|
80
81
|
describe "with ruby user" do
|
81
82
|
before do
|
82
83
|
@user = RubyUser.new
|
83
|
-
@user.first_name =
|
84
|
+
@user.first_name = " joe "
|
84
85
|
@user.last_name = "\n black\n"
|
85
86
|
@user.address1 = "2632 Brown St \n"
|
86
87
|
end
|
87
88
|
|
88
|
-
it
|
89
|
+
it "cleanse_attributes! using global cleaner" do
|
89
90
|
@user.cleanse_attributes!
|
90
|
-
assert_equal
|
91
|
-
assert_equal
|
91
|
+
assert_equal "joe", @user.first_name
|
92
|
+
assert_equal "black", @user.last_name
|
92
93
|
end
|
93
94
|
|
94
|
-
it
|
95
|
+
it "cleanse_attributes! using attribute specific custom cleaner" do
|
95
96
|
@user.cleanse_attributes!
|
96
|
-
assert_equal
|
97
|
+
assert_equal "<< 2632 Brown St >>", @user.address1
|
97
98
|
end
|
98
99
|
|
99
|
-
it
|
100
|
+
it "cleanse_attributes! not cleanse nil attributes" do
|
100
101
|
@user.first_name = nil
|
101
102
|
@user.cleanse_attributes!
|
102
|
-
|
103
|
+
assert_nil @user.first_name
|
103
104
|
end
|
104
105
|
|
105
|
-
it
|
106
|
-
@user.first_name =
|
106
|
+
it "cleanse_attributes! call after cleaner" do
|
107
|
+
@user.first_name = "Jack"
|
107
108
|
@user.last_name = nil
|
108
109
|
@user.cleanse_attributes!
|
109
|
-
|
110
|
-
assert_equal
|
110
|
+
assert_nil @user.first_name, @user.inspect
|
111
|
+
assert_equal "Jack", @user.last_name, @user.inspect
|
111
112
|
end
|
112
113
|
end
|
113
114
|
|
114
115
|
describe "with ruby user child" do
|
115
116
|
before do
|
116
117
|
@user = RubyUserChild.new
|
117
|
-
@user.first_name =
|
118
|
+
@user.first_name = " joe "
|
118
119
|
@user.last_name = "\n black\n"
|
119
120
|
@user.address1 = "2632 Brown St \n"
|
120
121
|
@user.gender = "\n Male \n"
|
121
122
|
end
|
122
123
|
|
123
|
-
it
|
124
|
+
it "cleanse_attributes! using global cleaner" do
|
124
125
|
@user.cleanse_attributes!
|
125
|
-
assert_equal
|
126
|
-
assert_equal
|
126
|
+
assert_equal "joe", @user.first_name
|
127
|
+
assert_equal "black", @user.last_name
|
127
128
|
end
|
128
129
|
|
129
|
-
it
|
130
|
+
it "cleanse_attributes! using attribute specific custom cleaner" do
|
130
131
|
@user.cleanse_attributes!
|
131
|
-
assert_equal
|
132
|
+
assert_equal "<< 2632 Brown St >>", @user.address1
|
132
133
|
end
|
133
134
|
|
134
|
-
it
|
135
|
+
it "cleanse_attributes! not cleanse nil attributes" do
|
135
136
|
@user.first_name = nil
|
136
137
|
@user.cleanse_attributes!
|
137
|
-
|
138
|
+
assert_nil @user.first_name
|
138
139
|
end
|
139
140
|
|
140
|
-
it
|
141
|
+
it "cleanse_attributes! clean child attributes" do
|
141
142
|
@user.cleanse_attributes!
|
142
|
-
assert_equal
|
143
|
+
assert_equal "male", @user.gender
|
143
144
|
end
|
144
|
-
|
145
145
|
end
|
146
146
|
|
147
147
|
describe "with ruby user2" do
|
148
148
|
before do
|
149
149
|
@user = RubyUser2.new
|
150
|
-
@user.first_name =
|
150
|
+
@user.first_name = " joe "
|
151
151
|
@user.last_name = "\n black\n"
|
152
152
|
@user.address1 = "2632 Brown St \n"
|
153
153
|
@user.title = " \nmr \n"
|
154
154
|
@user.gender = " Unknown "
|
155
155
|
end
|
156
156
|
|
157
|
-
it
|
157
|
+
it "cleanse_attributes!" do
|
158
158
|
@user.cleanse_attributes!
|
159
|
-
assert_equal
|
160
|
-
assert_equal
|
161
|
-
assert_equal
|
159
|
+
assert_equal "joe", @user.first_name
|
160
|
+
assert_equal "black", @user.last_name
|
161
|
+
assert_equal "2632 Brown St", @user.address1
|
162
162
|
end
|
163
163
|
|
164
|
-
it
|
164
|
+
it "cleanse_attributes! with multiple cleaners" do
|
165
165
|
@user.cleanse_attributes!
|
166
|
-
assert_equal
|
166
|
+
assert_equal "MR.", @user.title
|
167
167
|
end
|
168
168
|
|
169
|
-
it
|
169
|
+
it "cleanse_attributes! referencing other attributes" do
|
170
170
|
@user.cleanse_attributes!
|
171
|
-
assert_equal
|
171
|
+
assert_equal "Male", @user.gender
|
172
172
|
end
|
173
173
|
end
|
174
|
-
|
175
174
|
end
|
176
175
|
end
|
data/test/test_db.sqlite3
CHANGED
Binary file
|
data/test/test_helper.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require 'awesome_print'
|
8
|
-
require 'data_cleansing'
|
3
|
+
require "active_record"
|
4
|
+
require "minitest/autorun"
|
5
|
+
require "data_cleansing"
|
6
|
+
require "amazing_print"
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
SemanticLogger.add_appender(file_name: 'test.log', formatter: :color)
|
8
|
+
SemanticLogger.add_appender(file_name: "test.log", formatter: :color)
|
13
9
|
SemanticLogger.default_level = :debug
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_cleansing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -38,9 +38,8 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.0'
|
41
|
-
description:
|
41
|
+
description:
|
42
42
|
email:
|
43
|
-
- reidmo@gmail.com
|
44
43
|
executables: []
|
45
44
|
extensions: []
|
46
45
|
extra_rdoc_files: []
|
@@ -56,6 +55,7 @@ files:
|
|
56
55
|
- lib/data_cleansing/version.rb
|
57
56
|
- test/active_record_test.rb
|
58
57
|
- test/cleaners_test.rb
|
58
|
+
- test/data_cleansing_test.rb
|
59
59
|
- test/ruby_test.rb
|
60
60
|
- test/test_db.sqlite3
|
61
61
|
- test/test_helper.rb
|
@@ -63,7 +63,7 @@ homepage: http://github.com/reidmorrison/data_cleansing
|
|
63
63
|
licenses:
|
64
64
|
- Apache-2.0
|
65
65
|
metadata: {}
|
66
|
-
post_install_message:
|
66
|
+
post_install_message:
|
67
67
|
rdoc_options: []
|
68
68
|
require_paths:
|
69
69
|
- lib
|
@@ -71,21 +71,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
71
|
requirements:
|
72
72
|
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
74
|
+
version: '2.3'
|
75
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
76
|
requirements:
|
77
77
|
- - ">="
|
78
78
|
- !ruby/object:Gem::Version
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
|
-
|
82
|
-
|
83
|
-
signing_key:
|
81
|
+
rubygems_version: 3.2.22
|
82
|
+
signing_key:
|
84
83
|
specification_version: 4
|
85
|
-
summary: Data Cleansing framework for Ruby, Rails,
|
84
|
+
summary: Data Cleansing framework for Ruby, Rails, and Mongoid.
|
86
85
|
test_files:
|
87
86
|
- test/active_record_test.rb
|
88
87
|
- test/cleaners_test.rb
|
88
|
+
- test/data_cleansing_test.rb
|
89
89
|
- test/ruby_test.rb
|
90
90
|
- test/test_db.sqlite3
|
91
91
|
- test/test_helper.rb
|