Class: Seedie::ModelSeeder

Inherits:
Object
  • Object
show all
Includes:
Reporters::Reportable
Defined in:
lib/seedie/model_seeder.rb

Constant Summary collapse

DEFAULT_MODEL_COUNT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reporters::Reportable

#add_observers, #report

Constructor Details

#initialize(model, model_config, config, reporters) ⇒ ModelSeeder

Returns a new instance of ModelSeeder.



11
12
13
14
15
16
17
18
# File 'lib/seedie/model_seeder.rb', line 11

def initialize(model, model_config, config, reporters)
  @model = model
  @model_config = model_config
  @config = config
  @record_creator = Model::Creator.new(model, reporters)
  @reporters = reporters
  add_observers(@reporters)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/seedie/model_seeder.rb', line 9

def config
  @config
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/seedie/model_seeder.rb', line 9

def model
  @model
end

#model_configObject (readonly)

Returns the value of attribute model_config.



9
10
11
# File 'lib/seedie/model_seeder.rb', line 9

def model_config
  @model_config
end

#reportersObject (readonly)

Returns the value of attribute reporters.



9
10
11
# File 'lib/seedie/model_seeder.rb', line 9

def reporters
  @reporters
end

Instance Method Details

#generate_recordsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/seedie/model_seeder.rb', line 20

def generate_records
  report(:model_seed_start, name: model.to_s)
  model_count(model_config).times do |index|
    record = generate_record(model_config, index)
    associations_config = model_config["associations"]

    next unless associations_config.present?

    Associations::HasMany.new(record, model, associations_config, reporters).generate_associations
    Associations::HasAndBelongsToMany.new(record, model, associations_config, reporters).generate_associations
    Associations::HasOne.new(record, model, associations_config, reporters).generate_associations
  end
  report(:model_seed_finish, name: model.to_s)
end