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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11b8ffae00bf6ea254e0fbcf8792b70b5bde6a6b
4
- data.tar.gz: 0fffb33a679f705e73cce112a6809dd516123db5
3
+ metadata.gz: ba13d73dba1f1632c1f2deb70e6955117909e0ec
4
+ data.tar.gz: c96834de7971994218de45c2391cc73875959fe5
5
5
  SHA512:
6
- metadata.gz: 7c577a730468fbbb87513688325c86f253357bdc4f86e627bf7f146df44b6b8d3230737a42a42c0a760d55d5e0aaeeb1022fbf9c20e49a81afea7d6961c3c944
7
- data.tar.gz: 5cb90c0a5c1520f93c93c0a43b9579fa4cca529a590839af0f767a5297f5ff80218f366eb07b3ec83e6d17862c3517da493bb08b1097f6f6e62e34191ced8b52
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.
@@ -74,6 +74,7 @@ module FakedCSV
74
74
  when /rand:char/i
75
75
  field[:type] = :rand_char
76
76
  field[:length] = cfg["length"].nil? ? 10 : cfg["length"]
77
+ field[:format] = cfg["format"]
77
78
  when /fixed/i
78
79
  field[:type] = :fixed
79
80
  raise "need values for fixed type" if cfg["values"].nil?
@@ -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
- return Generator.rand_char field[:length]
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)
@@ -1,3 +1,3 @@
1
1
  module FakedCSV
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -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},
@@ -4,7 +4,8 @@
4
4
  {
5
5
  "name": "ID",
6
6
  "type": "rand:char",
7
- "length": 5
7
+ "length": 5,
8
+ "format": "001/W/wabc"
8
9
  },
9
10
  {
10
11
  "name": "First Name",
@@ -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.2
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-06-10 00:00:00.000000000 Z
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler