Class: RubyUiScaffold::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ui_scaffold/seeder.rb

Overview

Orchestrates seeding N records for a given ActiveRecord model. Delegates per-attribute value generation to ValueGenerator.

Examples:

seeder = RubyUiScaffold::Seeder.new(Buddy, count: 50)
seeder.run  # => prints progress, returns count of created records

Constant Summary collapse

MAX_RETRIES =
3
PROGRESS_EVERY =
10

Instance Method Summary collapse

Constructor Details

#initialize(model_class, count:, reset: false, dry_run: false, io: $stdout) ⇒ Seeder

Returns a new instance of Seeder.



18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_ui_scaffold/seeder.rb', line 18

def initialize(model_class, count:, reset: false, dry_run: false, io: $stdout)
  @model_class = model_class
  @count = count
  @reset = reset
  @dry_run = dry_run
  @io = io
  @created = 0
  @failed = 0
  @errors = []
end

Instance Method Details

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_ui_scaffold/seeder.rb', line 29

def run
  return dry_run! if @dry_run

  preflight!
  reset_table! if @reset

  started = Time.now
  @io.puts "Seeding #{@count} #{@model_class} records..."

  @count.times do |i|
    record = attempt_create
    if record
      @created += 1
      report_progress(i + 1, record)
    else
      @failed += 1
    end
  end

  report_summary(Time.now - started)
  @created
end