Class: ActiveWarehouse::Builder::RandomDataBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_warehouse/builder/random_data_builder.rb

Overview

Build random data usable for testing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRandomDataBuilder

Initialize the random data builder



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 12

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_generatorsObject (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

#generatorsObject (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

Build the data for the specified class. Name may be a Class (which must descend from ActiveWarehouse::Dimension or ActiveWarehouse::Fact), a String or a Symbol. String or Symbol will be converted to a class name and then passed back to this method.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 27

def build(name, options={})
  case name
  when Class
    if name.respond_to?(:base_class)
      return build_dimension(name, options) if name.base_class == ActiveWarehouse::Dimension
      return build_fact(name, options) 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, options)
    rescue NameError
      raise "Cannot find a class named #{name.classify}"
    end
  when Symbol
    build(name.to_s, options)
  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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 55

def build_dimension(name, options={})
  options[:rows] ||= 100
  options[:generators] ||= {}
  rows = []

  dimension_class = Dimension.to_dimension(name)
  options[:rows].times do
    row = {}
    dimension_class.content_columns.each do |column|
      generator = (options[:generators][column.klass] || @column_generators[column.name] || @generators[column.klass])
      if generator.nil?
        raise ArgumentError, "No generator found, unknown column type?: #{column.klass}"
      end
      row[column.name] = generator.generate(column, options)
    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.

  • :dimensions: The number of available dimension FKs



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 86

def build_fact(name, options={})
  options[:rows] ||= 100
  options[:generators] ||= {}
  options[:fk_limit] ||= {}
  rows = []

  fact_class = Fact.to_fact(name)
  options[:rows].times do
    row = {}
    fact_class.content_columns.each do |column|
      generator = (options[:generators][column.klass] || @generators[column.klass])
      row[column.name] = generator.generate(column, options)
    end
    fact_class.dimension_relationships.each do |name, reflection|
      # it would be better to get a count of rows from the dimension tables
      fk_limit = (options[:fk_limit][reflection.primary_key_name] || options[:dimensions] || 100) - 1
      row[reflection.primary_key_name] = rand(fk_limit) + 1
    end
    rows << row
  end
  
  rows
end