Class: FakedCSV::Generator
- Inherits:
-
Object
- Object
- FakedCSV::Generator
- Defined in:
- lib/faked_csv/generator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Class Method Summary collapse
- .fake(type) ⇒ Object
-
.rand_char(length) ⇒ Object
individual random generators.
- .rand_float(min, max, precision) ⇒ Object
- .rand_int(min, max) ⇒ Object
Instance Method Summary collapse
- #_loop ⇒ Object
- #_random_distribution(total, parts) ⇒ Object
-
#_random_inject(values, injects) ⇒ Object
inject <injects> into <values>.
- #_random_value(field) ⇒ Object
- #generate ⇒ Object
- #headers ⇒ Object
-
#initialize(config) ⇒ Generator
constructor
A new instance of Generator.
- #prepare_values ⇒ Object
Constructor Details
#initialize(config) ⇒ Generator
Returns a new instance of Generator.
5 6 7 |
# File 'lib/faked_csv/generator.rb', line 5 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/faked_csv/generator.rb', line 3 def config @config end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
3 4 5 |
# File 'lib/faked_csv/generator.rb', line 3 def rows @rows end |
Class Method Details
.fake(type) ⇒ Object
172 173 174 |
# File 'lib/faked_csv/generator.rb', line 172 def self.fake(type) Fakerer.new(type).fake end |
.rand_char(length) ⇒ Object
individual random generators
157 158 159 160 |
# File 'lib/faked_csv/generator.rb', line 157 def self.rand_char(length) o = [('a'..'z'), ('A'..'Z'), (0..9)].map { |i| i.to_a }.flatten string = (0...length).map { o[rand(o.length)] }.join end |
.rand_float(min, max, precision) ⇒ Object
167 168 169 170 |
# File 'lib/faked_csv/generator.rb', line 167 def self.rand_float(min, max, precision) raise "min > max" if min > max (rand * (max - min) + min).round(precision) end |
.rand_int(min, max) ⇒ Object
162 163 164 165 |
# File 'lib/faked_csv/generator.rb', line 162 def self.rand_int(min, max) raise "min > max" if min > max min + rand(max - min + 1) end |
Instance Method Details
#_loop ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/faked_csv/generator.rb', line 145 def _loop max_attempts = 1000_000_000_000 i = 0 (0...max_attempts).each do |j| yield i += 1 end raise "max attempts reached" if i == max_attempts end |
#_random_distribution(total, parts) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/faked_csv/generator.rb', line 91 def _random_distribution(total, parts) raise "parts has to be greater than 0" unless parts > 0 raise "parts should not be greater than total" if total < parts cuts = {} _loop do break if cuts.size == parts - 1 cuts[rand(total - 1)] = true end arr = [] part_index = 0 (0...total).each do |i| arr << part_index part_index += 1 if cuts.has_key? i end arr.shuffle.each_with_index do |v, i| yield(i, v) end end |
#_random_inject(values, injects) ⇒ Object
inject <injects> into <values>
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/faked_csv/generator.rb', line 111 def _random_inject(values, injects) used_indexes = {} count = injects.size > values.size ? values.size : injects.size (0...count).each do |i| inj = injects[i] times_inject = rand(values.size / injects.size / 10) times_inject = 1 if times_inject < 1 times_inject.times do rand_index = rand(values.size) _loop do break unless used_indexes.has_key? rand_index rand_index = rand(values.size) end used_indexes[rand_index] = true values[rand_index] = inj end end end |
#_random_value(field) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/faked_csv/generator.rb', line 130 def _random_value(field) case field[:type] when :rand_int return Generator.rand_int field[:min], field[:max] when :rand_float return Generator.rand_float field[:min], field[:max], field[:precision] when :rand_char return Generator.rand_char field[:length] when :fixed return field[:values].sample else # faker return Generator.fake field[:type] end end |
#generate ⇒ Object
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 |
# File 'lib/faked_csv/generator.rb', line 26 def generate prepare_values @config.fields.each do |field| field[:data] = [] # let's get some data! if field[:type] == :inc_int i = field[:start] @config.row_count.times do field[:data] << i i += field[:step] end elsif field[:rotate].nil? || field[:type] == :fixed # not rotating? or fixed values? generate random value each time @config.row_count.times do field[:data] << _random_value(field) end # inject user values if given and not fixed type unless field[:type] == :fixed || field[:inject].nil? _random_inject(field[:data], field[:inject]) end else # rotating? pick from prepared values _random_distribution(@config.row_count, field[:values].size) do |i, j| field[:data][i] = field[:values][j] end end end end |
#headers ⇒ Object
9 10 11 |
# File 'lib/faked_csv/generator.rb', line 9 def headers @config.headers end |
#prepare_values ⇒ Object
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 |
# File 'lib/faked_csv/generator.rb', line 58 def prepare_values @config.fields.each do |field| # if it's fixed values or no rotate # we don't want to prepare values for this field if [:inc_int, :fixed].include?(field[:type]) || field[:rotate].nil? next end # we don't have enough integers for the rotate if field[:type] == :rand_int && field[:rotate] > field[:max] - field[:min] + 1 raise "rotate should not be greater than the size of the range" end values = {} # let's first inject all user values if given unless field[:inject].nil? field[:inject].each do |inj| values[inj] = true # truncate more inject values if we go over the rows count break if values.size == @config.row_count end end # then generate as many data as we need _loop do # we want to get <rotate> unique values. stop when we got enough break if values.size >= field[:rotate] v = _random_value(field) values[v] = true end field[:values] = values.keys end end |