Class: ActiveWarehouse::Builder::TestDataBuilder
- Inherits:
-
Object
- Object
- ActiveWarehouse::Builder::TestDataBuilder
- Defined in:
- lib/active_warehouse/builder/test_data_builder.rb
Overview
Unlike the RandomDataBuilder, which puts truly random data in the warehouse, this generator uses collections of possible values to construct semi-understandable data
Instance Method Summary collapse
-
#build(fields, field_definitions, options = {}) ⇒ Object
Usage: .
-
#initialize ⇒ TestDataBuilder
constructor
A new instance of TestDataBuilder.
Constructor Details
#initialize ⇒ TestDataBuilder
Returns a new instance of TestDataBuilder.
8 9 10 |
# File 'lib/active_warehouse/builder/test_data_builder.rb', line 8 def initialize end |
Instance Method Details
#build(fields, field_definitions, options = {}) ⇒ Object
Usage:
fields = [:id,:product_name,:product_description,:suggested_retail_price]
field_definitions = {
:id => :sequence, # symbol or string
:product_name => [['Foo','Bar']['Baz','Bing']], # array
:product_description => IpsumLorumGenerator # class
:suggested_retail_price => RandomNumberGenerator.new(0.00, 100.00) # generator instance
}
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 |
# File 'lib/active_warehouse/builder/test_data_builder.rb', line 21 def build(fields, field_definitions, ={}) [:number] ||= 100 rows = [] generators = {} # set up all of the generators first field_definitions.each do |name, fd| case fd when Class generators[name] = fd.new when String, Symbol generators[name] = "#{fd}Generator".classify.constantize.new when Array generators[name] = NameGenerator.new(fd) when Generator generators[name] = fd else raise "Invalid generator specified: #{fd}" end end # generate all of the rows 0.upto([:number]) do row = {} fields.each do |field| row[field] = generators[field].next() end rows << row end rows end |