Class: ActiveWarehouse::Builder::RandomDataBuilder
- Inherits:
-
Object
- Object
- ActiveWarehouse::Builder::RandomDataBuilder
- Defined in:
- lib/active_warehouse/builder/random_data_builder.rb
Overview
Build random data usable for testing.
Instance Attribute Summary collapse
-
#column_generators ⇒ Object
readonly
Hash of names mapped to generators where the name is the column name.
-
#generators ⇒ Object
readonly
Hash of generators where the key is the class and the value is an implementation of AbstractGenerator.
Instance Method Summary collapse
- #build(name, options = {}) ⇒ Object
-
#build_dimension(name, options = {}) ⇒ Object
Build test dimension data for the specified dimension name.
-
#build_fact(name, options = {}) ⇒ Object
Build test fact data for the specified fact name.
-
#initialize ⇒ RandomDataBuilder
constructor
A new instance of RandomDataBuilder.
Constructor Details
#initialize ⇒ RandomDataBuilder
Returns a new instance of RandomDataBuilder.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 11 def initialize @generators = { Fixnum => FixnumGenerator.new, Float => FloatGenerator.new, Date => DateGenerator.new, Time => TimeGenerator.new, String => StringGenerator.new, Object => BooleanGenerator.new, } @column_generators = {} end |
Instance Attribute Details
#column_generators ⇒ Object (readonly)
Hash of names mapped to generators where the name is the column name
9 10 11 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 9 def column_generators @column_generators end |
#generators ⇒ Object (readonly)
Hash of generators where the key is the class and the value is an implementation of AbstractGenerator
6 7 8 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 6 def generators @generators end |
Instance Method Details
#build(name, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 23 def build(name, ={}) case name when Class if name.respond_to?(:base_class) return build_dimension(name, ) if name.base_class == ActiveWarehouse::Dimension return build_fact(name, ) if name.base_class == ActiveWarehouse::Fact end raise "#{name} is a class but does not appear to descend from Fact or Dimension" when String begin build(name.classify.constantize, ) rescue NameError raise "Cannot find a class named #{name.classify}" end when Symbol build(name.to_s, ) else raise "Unable to determine what to build" end end |
#build_dimension(name, options = {}) ⇒ Object
Build test dimension data for the specified dimension name.
Options:
-
:rows
: The number of rows to create (defaults to 100) -
:generators
: A map of generators where each key is Fixnum, Float, Date, Time, String, or Object and the value is extends from AbstractGenerator.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 50 def build_dimension(name, ={}) [:rows] ||= 100 [:generators] ||= {} rows = [] dimension_class = Dimension.to_dimension(name) [:rows].times do row = {} dimension_class.content_columns.each do |column| generator = ([:generators][column.klass] || @column_generators[column.name] || @generators[column.klass]) row[column.name] = generator.generate(column, ) end rows << row end rows end |
#build_fact(name, options = {}) ⇒ Object
Build test fact data for the specified fact name
Options:
-
:rows
: The number of rows to create (defaults to 100) -
:generators
: A Hash of generators where each key is Fixnum, Float, Date, Time, String, or Object and the value is extends from AbstractGenerator. -
:fk_limit
: A Hash of foreign key limits, where each key is the name of column and the value is a number. For example options[:date_id] = 1000 would limit the foreign key values to something between 1 and 1000, inclusive.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 77 def build_fact(name, ={}) [:rows] ||= 100 [:generators] ||= {} [:fk_limit] ||= {} rows = [] fact_class = Fact.to_fact(name) [:rows].times do row = {} fact_class.content_columns.each do |column| generator = ([:generators][column.klass] || @generators[column.klass]) row[column.name] = generator.generate(column, ) end fact_class.foreign_key_columns.each do |column| fk_limit = ([:fk_limit][column.name] || 100) - 1 row[column.name] = rand(fk_limit) + 1 end rows << row end rows end |