Class: BetterSeeder::Farms::Farmer
- Inherits:
-
Object
- Object
- BetterSeeder::Farms::Farmer
- Defined in:
- lib/better_seeder/farms/farmer.rb
Class Method Summary collapse
-
.build_record(_model_name, structure_class, index, child_mode: false) ⇒ Object
Il metodo build_record ora supporta la modalità child_mode.
-
.child_record_count(options = {}) ⇒ Object
Restituisce il numero di record figli da generare per ciascun “record padre”.
- .generate(options = {}) ⇒ Object
Class Method Details
.build_record(_model_name, structure_class, index, child_mode: false) ⇒ Object
Il metodo build_record ora supporta la modalità child_mode. Se child_mode è true e nella configurazione (seed_config) è definita la chiave childs con :attributes, per ogni attributo viene usato il valore dell’array corrispondente all’indice (child_index) passato.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/better_seeder/farms/farmer.rb', line 77 def build_record(_model_name, structure_class, index, child_mode: false) generation_rules = structure_class.structure raise 'Structure must be a Hash' unless generation_rules.is_a?(Hash) seed_config = structure_class.respond_to?(:seed_config) ? structure_class.seed_config : {} record = {} generation_rules.each do |attribute, rule| generator = rule[1] if child_mode && seed_config.dig(:childs, :attributes, attribute).is_a?(Array) values = seed_config[:childs][:attributes][attribute] value = values[index] # index viene passato dal loop interno else value = generator.respond_to?(:call) ? generator.call : generator end record[attribute] = value end record end |
.child_record_count(options = {}) ⇒ Object
Restituisce il numero di record figli da generare per ciascun “record padre”. Nel caso in cui nella configurazione sia presente la chiave childs, restituisce childs, altrimenti default a 10.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/better_seeder/farms/farmer.rb', line 101 def child_record_count( = {}) model_name = [:model] or raise ArgumentError, 'Missing :model option' structure_file = File.( File.join(BetterSeeder.configuration.structure_path, "#{model_name.underscore}_structure.rb"), Dir.pwd ) raise "Structure file not found: #{structure_file}" unless File.exist?(structure_file) load structure_file structure_class_name = "#{model_name}Structure" structure_class = Object.const_get(structure_class_name) seed_config = structure_class.respond_to?(:seed_config) ? structure_class.seed_config : {} seed_config.dig(:childs, :count) || 10 end |
.generate(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/better_seeder/farms/farmer.rb', line 7 def generate( = {}) model_name = [:model] or raise ArgumentError, 'Missing :model option' # Costruisce il percorso del file di structure. structure_file = File.( File.join(BetterSeeder.configuration.structure_path, "#{model_name.underscore}_structure.rb"), Dir.pwd ) raise "Structure file not found: #{structure_file}" unless File.exist?(structure_file) load structure_file structure_class_name = "#{model_name}Structure" begin structure_class = Object.const_get(structure_class_name) rescue error = "Structure class not found: #{structure_class_name}" BetterSeeder::Utils.logger(message: ) raise error end seed_config = structure_class.respond_to?(:seed_config) ? structure_class.seed_config : {} total_count = seed_config[:count] || 10 generated_records = [] # Se il metodo preflight è definito, usalo per ottenere dati predefiniti if structure_class.respond_to?(:preflight) preflight_data = structure_class.preflight generated_records.concat(preflight_data) end remaining_count = total_count - generated_records.size if seed_config.key?(:childs) # Modalità child: per ogni "record padre", generare childs_count record childs_count = seed_config.dig(:childs, :count) || 10 # Calcolo: i record generati saranno i preflight + (remaining_count * childs_count) remaining_count.times do |_i| childs_count.times do |child_index| new_record = nil loop do new_record = build_record(model_name, structure_class, child_index, child_mode: true) new_record = inject_parent_keys(model_name, new_record, structure_class) break if validate_record(new_record, structure_class) && !record_exists?(model_name, new_record, structure_class, generated_records) end generated_records.push(new_record) end end else # Modalità standard: genera remaining_count record remaining_count.times do |index| new_record = nil loop do new_record = build_record(model_name, structure_class, index) new_record = inject_parent_keys(model_name, new_record, structure_class) break if validate_record(new_record, structure_class) && !record_exists?(model_name, new_record, structure_class, generated_records) end generated_records.push(new_record) end end generated_records end |