Class: ActiveWarehouse::Builder::StringGenerator

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

Overview

A basic String generator

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StringGenerator

Initialize the StringGenerator.

Options:

  • :values: List of possible values

  • :chars: List of chars to use to generate random values



192
193
194
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 192

def initialize(options={})
  @options = options
end

Instance Method Details

#generate(column, options = {}) ⇒ Object

Generate a random string

Options:

  • :values: An array of values to use. If not specified then random char values will be used.

  • :chars: An array of characters to use to generate random values (default [a..zA..Z])



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/active_warehouse/builder/random_data_builder.rb', line 200

def generate(column, options={})
  options[:values] ||= @options[:values]
  options[:chars] ||= @options[:chars]
  if options[:values]
    options[:values][rand(options[:values].length)]
  else
    s = ''
    chars = (options[:chars] || ('a'..'z').to_a + ('A'..'Z').to_a)
    0.upto(column.limit - 1) do |n|
      s << chars[rand(chars.length)]
    end
    s
  end
end