faked_csv 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/faked_csv/config.rb +1 -0
- data/lib/faked_csv/generator.rb +40 -1
- data/lib/faked_csv/version.rb +1 -1
- data/spec/config_spec.rb +1 -1
- data/spec/data/basic.csv.json +2 -1
- data/spec/generator_spec.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba13d73dba1f1632c1f2deb70e6955117909e0ec
|
4
|
+
data.tar.gz: c96834de7971994218de45c2391cc73875959fe5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a4af984ff6284ee06704d91aac6f43067e785856d3934ae3c12998b955b7e9dcc5587eeb6096cdad27af4d23620b52b010dc92257b2dea71cb74d8c5e76c23f
|
7
|
+
data.tar.gz: e991598276b72999e178cfc4feee36f6676c1c4641e458fe6c41a42dbb95349d5cadc5a009b1f82913afb187a59df829fca5e0db89864d9d94db68449b2c194e
|
data/README.md
CHANGED
@@ -183,6 +183,18 @@ Random characters. Only A-Z, a-z and 0-9 will be considered as characters.
|
|
183
183
|
|
184
184
|
`length` to specify how many characters in each of the generated data, e.g. 15 will give you something like `9mLcZHR9H5V7pg9`. This is particularly useful if you want to mimic something like item ID, access token etc.
|
185
185
|
|
186
|
+
`format` to sepcify the format of the randomly generated strings. Any character in the format string will be print to the generated string unless it's prefixed with a '/' which will generate a corresponding character based on the format given after the '/' sign.
|
187
|
+
|
188
|
+
`/w` any letter between 'a' to 'z'
|
189
|
+
`/W` any letter between 'A' to 'Z'
|
190
|
+
`/D` any letter between 'A' to 'Z' or 'a' to 'z'
|
191
|
+
`/d` any digit between 0 to 9
|
192
|
+
`/@` alphanumeric, A-Z, a-z, or 0-9
|
193
|
+
|
194
|
+
Example:
|
195
|
+
|
196
|
+
`001/W/W/d/@` might generate something like: `001HR9a`
|
197
|
+
|
186
198
|
#### faker:[class]:[method]
|
187
199
|
|
188
200
|
This indicates you want to use one of the methods from Faker gem. You can find the list of all methods from [Faker gem's documentation](http://rubydoc.info/github/stympy/faker). Each method is a class name and a method name. Example, `Faker::Internet.url` can be mapped to `faker:internet:url` as a field type in the configuration. As current version, we don't support parameters into the faker method. Future version will add such support.
|
data/lib/faked_csv/config.rb
CHANGED
data/lib/faked_csv/generator.rb
CHANGED
@@ -134,7 +134,11 @@ module FakedCSV
|
|
134
134
|
when :rand_float
|
135
135
|
return Generator.rand_float field[:min], field[:max], field[:precision]
|
136
136
|
when :rand_char
|
137
|
-
|
137
|
+
if field[:format].nil?
|
138
|
+
return Generator.rand_char field[:length]
|
139
|
+
else
|
140
|
+
return Generator.rand_formatted_char field[:format]
|
141
|
+
end
|
138
142
|
when :fixed
|
139
143
|
return field[:values].sample
|
140
144
|
else # faker
|
@@ -159,6 +163,41 @@ module FakedCSV
|
|
159
163
|
string = (0...length).map { o[rand(o.length)] }.join
|
160
164
|
end
|
161
165
|
|
166
|
+
def self.rand_formatted_char(format)
|
167
|
+
res = []
|
168
|
+
i = 0
|
169
|
+
while i < format.size
|
170
|
+
case a = format[i]
|
171
|
+
when '/'
|
172
|
+
i += 1
|
173
|
+
res << single_rand_char(format[i])
|
174
|
+
else
|
175
|
+
res << a
|
176
|
+
end
|
177
|
+
i += 1
|
178
|
+
end
|
179
|
+
return res.join("")
|
180
|
+
end
|
181
|
+
|
182
|
+
def self.single_rand_char(format)
|
183
|
+
aa = nil
|
184
|
+
case format
|
185
|
+
when 'W' # A-Z
|
186
|
+
aa = ('A'..'Z').to_a
|
187
|
+
when 'w' # a-z
|
188
|
+
aa = ('a'..'z').to_a
|
189
|
+
when 'd' # 0-9
|
190
|
+
aa = (0..9).to_a
|
191
|
+
when 'D' # A-Za-z
|
192
|
+
aa = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
|
193
|
+
when '@'
|
194
|
+
aa = [('a'..'z'), ('A'..'Z'), (0..9)].map { |i| i.to_a }.flatten
|
195
|
+
else
|
196
|
+
raise "invalid format: #{format} in single_rand_char"
|
197
|
+
end
|
198
|
+
return aa[rand(aa.size)]
|
199
|
+
end
|
200
|
+
|
162
201
|
def self.rand_int(min, max)
|
163
202
|
raise "min > max" if min > max
|
164
203
|
min + rand(max - min + 1)
|
data/lib/faked_csv/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe FakedCSV::Config do
|
|
8
8
|
config.parse
|
9
9
|
config.row_count.should == 200
|
10
10
|
config.fields.should == [
|
11
|
-
{:name=>"ID", :type=>:rand_char, :length=>5},
|
11
|
+
{:name=>"ID", :type=>:rand_char, :length=>5, :format=>"001/W/wabc"},
|
12
12
|
{:name=>"First Name", :type=>:fixed, :values=>["Peter", "Tom", "Jane", "Tony", "Steve", "John"]},
|
13
13
|
{:name=>"Last Name", :type=>"faker:name:last_name", :inject=>["Lu", "Smith"]},
|
14
14
|
{:name=>"City", :type=>"faker:address:city", :rotate=>40},
|
data/spec/data/basic.csv.json
CHANGED
data/spec/generator_spec.rb
CHANGED
@@ -7,6 +7,26 @@ describe FakedCSV::Generator do
|
|
7
7
|
FakedCSV::Generator.rand_char(100).size.should == 100
|
8
8
|
end
|
9
9
|
|
10
|
+
it "generates single rand char with format" do
|
11
|
+
('A'..'Z').to_a.include?(FakedCSV::Generator.single_rand_char('W')).should == true
|
12
|
+
('a'..'z').to_a.include?(FakedCSV::Generator.single_rand_char('w')).should == true
|
13
|
+
(0..9).to_a.include?(FakedCSV::Generator.single_rand_char('d')).should == true
|
14
|
+
[('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten.include?(FakedCSV::Generator.single_rand_char('D')).should == true
|
15
|
+
[('a'..'z'), ('A'..'Z'), (0..9)].map { |i| i.to_a }.flatten.include?(FakedCSV::Generator.single_rand_char('@')).should == true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "generates rand chars with format" do
|
19
|
+
a = FakedCSV::Generator.rand_formatted_char("123/w123")
|
20
|
+
a.size.should == 7
|
21
|
+
('a'..'z').to_a.include?(a[3]).should == true
|
22
|
+
|
23
|
+
a = FakedCSV::Generator.rand_formatted_char("/W/w/d/D/@")
|
24
|
+
a.size.should == 5
|
25
|
+
|
26
|
+
a = FakedCSV::Generator.rand_formatted_char("0014000000/@/@/@/@/@/W/W/W")
|
27
|
+
a.size.should == 18
|
28
|
+
end
|
29
|
+
|
10
30
|
it "generates rand int" do
|
11
31
|
1000.times do
|
12
32
|
i = FakedCSV::Generator.rand_int(0, 10)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faked_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jianan Lu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|