Class: GenerateData
- Inherits:
-
Object
- Object
- GenerateData
- Defined in:
- lib/fake_csv/g_data.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #append_result(result, string) ⇒ Object
- #generate(format) ⇒ Object
-
#initialize ⇒ GenerateData
constructor
A new instance of GenerateData.
Constructor Details
#initialize ⇒ GenerateData
Returns a new instance of GenerateData.
5 6 7 |
# File 'lib/fake_csv/g_data.rb', line 5 def initialize @data = [] end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/fake_csv/g_data.rb', line 4 def data @data end |
Instance Method Details
#append_result(result, string) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/fake_csv/g_data.rb', line 60 def append_result(result, string) if result.nil? result = string else result = result + "," + string end result end |
#generate(format) ⇒ Object
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 |
# File 'lib/fake_csv/g_data.rb', line 8 def generate(format) global_id = 0 format.each do |seq, index| count = index[:format].scan(/\(([^\)]+)\)/).last.first.to_i (1..count).each do |id| result = nil form = index[:content].split(',').map(&:strip) # remove surrounding whitespace form.each do |value| case value when 'id' global_id += 1 result = append_result(result, global_id.to_s) when 'name' result = append_result(result, Faker::Name.first_name) when 'email' result = append_result(result, Faker::Internet.email) else split = value.split('') if split.include?("$") or split.include?("#") string = "" split.each do |value| if value == "#" string += ('a'..'z').to_a.shuffle[0, 1].join elsif value == "$" string += (0..9).to_a.shuffle[0, 1].join else string += value end end result = append_result(result, string) elsif split.include?("~") # number range first = value.split("~")[0].to_i second = value.split("~")[1].to_i number = (first..second).to_a.shuffle[0,1].join result = append_result(result, number.to_s) elsif split.include?("[") # with require string value_ary = value.scan(/\[([^\)]+)\]/).last.first # get value in array value_ary = value_ary.split(/\s(?=(?:[^"]|"[^"]*")*$)/) suffle_value = value_ary.shuffle[0, 1].join.gsub /"/, '' result = append_result(result, suffle_value) else # if the value is default result = append_result(result, value) end end end @data.push(result) end end end |