-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.txt
196 lines (144 loc) · 5.86 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
= DataCleaner
DataCleaner is a library to aid you in anonymising your data, for example when attempting to reproduce a bug in development, and only live data will do, but it's not safe to have a whole database of customer names, email, etc on your development machine.
DataCleaner wants to make sure your data still looks real, and importantly, passes any validation your code might have. To achieve this it provides a DSL for you to specify the format of the data, along with helpers (using the faker gem) to generate common data.
Only data that need anonymising needs to be specified, foreign keys, non-customer-identifiable data should be left alone.
* rdoc[http://sourcetagsandcodes.com/data_cleaner/doc/]
* source[https://github.com/matsadler/data_cleaner]
== Installation
gem install data_cleaner
== Usage
require 'rubygems'
require 'data_cleaner'
class TopSecret
attr_accessor :name, :email, :reference, :secret, :date
def initialize(name, email, reference, secret, date)
@name = name
@email = email
@reference = reference
@secret = secret
@date = date
end
def valid?
name.match(/^[a-z]+ [a-z]+$/i) &&
reference.match(/^[a-z]{3}[0-9]{1,5}$/) &&
date.is_a?(Time) || false
end
end
module DataCleaner::Formats
helper :embarrassing_secret do
"I like " + ["the colour pink", "programming PHP", "Judas Priest"].sample
end
format "TopSecret" do |f|
f.name [:first_name, " ", :last_name]
f.email :email, &:name # passes the objects name to the email method
f.reference do |obj| # one off helper
"#{obj.name[0..2].downcase}#{obj.date.strftime("%y")}"
end
f.secret :embarrassing_secret
end
end
secret = TopSecret.new("Matthew Sadler", "[email protected]", "mat09", "I like kittens", Time.now)
puts secret.inspect
puts "is valid? #{secret.valid?}"
puts
clean = DataCleaner::Cleaner.clean!(secret)
puts clean.inspect
puts "is valid? #{clean.valid?}"
prints:
#<TopSecret:0x1015f7830 @email="[email protected]", @date=Mon Jan 17 16:53:19 +0000 2011, @name="Matthew Sadler", @secret="I like kittens", @reference="mat09">
is valid? true
#<TopSecret:0x1015f7830 @email="[email protected]", @date=Mon Jan 17 16:53:19 +0000 2011, @name="Javier Kuhlman", @secret="the colour pink", @reference="jav11">
is valid? true
== Formats
There are various ways of specifying the format of an attribute.
=== Basic Symbol
format "TopSecret" do |f|
f.name :first_name
end
In this case the helper :first_name will be used to replace the name attribute
=== Symbol With Arguments
format "TopSecret" do |f|
f.email :email, "Arthur"
end
The helper :email will be used to replace the attribute, and be given the argument "Arthur"
=== Symbol With Block
format "TopSecret" do |f|
f.name :first_name
f.email(:email) {|obj| obj.name}
end
In this example the :email helper will be given the objects replacement name as an argument
format "TopSecret" do |f|
f.email(:email) {|obj| obj.name}
f.name :first_name
end
Here the :email helper will get the objects original name as an argument
=== String
format "TopSecret" do |f|
f.name "Arthur"
end
In this case the name will simply be replaced with the string specified
=== Array
format "TopSecret" do |f|
f.name [:first_name, " ", :last_name]
end
With an array the individual elements behave like those above, then they are concatenated together, in this example the results from the :first_name and :last_name helpers will be joined with the string " " between them
=== Nested Arrays
format "TopSecret" do |f|
f.emails [[:email, "Arthur"], ", ", [:email, "Ford"]]
end
In this example the :email helper will be called twice, once with the argument "Arthur", then again with the argument "Ford", and these will be joined by the string ", "
=== Block
format "TopSecret" do |f|
f.secret {|obj| rand(100)}
end
When using a block the attribute will be replaced by the result of the block.
== Built-in helpers
The built-in helpers use the faker gem to generate data, see the faker documentation for more details
:name
:first_name
:last_name
:name_prefix
:name_suffix
:phone_number
:city
:city_prefix
:city_suffix
:secondary_address
:street_address
:street_name
:street_suffix
:uk_country
:uk_county
:uk_postcode
:us_state
:us_state_abbr
:zip_code
:domain_name
:domain_suffix
:domain_word
:email
:free_email
:user_name
:bs
:catch_phrase
:company_name
:company_suffix
:paragraph
:paragraphs
:sentence
:sentences
:words
== Custom helpers
Custom helpers can be defined like
module DataCleaner::Formats
helper :embarrassing_secret do
"I like " + ["the colour pink", "programming PHP", "Judas Priest"].sample
end
end
This can also be used to redefine the built-in helpers.
== Licence
(The MIT License)
Copyright © 2011 Matthew Sadler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.