Class: Populator::Factory
- Inherits:
-
Object
- Object
- Populator::Factory
- Defined in:
- lib/populator/factory.rb
Overview
Builds multiple Populator::Record instances and saves them to the database
Constant Summary collapse
- DEFAULT_RECORDS_PER_QUERY =
1000
Class Method Summary collapse
-
.for_model(model_class) ⇒ Object
Fetches the factory dedicated to a given model class.
-
.remember_depth ⇒ Object
Keep track of nested factory calls so we can save the remaining records once we are done with the base factory.
-
.save_remaining_records ⇒ Object
Find all remaining factories and call save_records on them.
Instance Method Summary collapse
-
#build_records(amount, per_query, &block) ⇒ Object
Builds multiple Populator::Record instances and calls save_records them when :per_query limit option is reached.
-
#initialize(model_class) ⇒ Factory
constructor
Use for_model instead of instatiating a record directly.
-
#populate(amount, options = {}, &block) ⇒ Object
Entry method for building records.
-
#save_records ⇒ Object
Saves the records to the database by calling populate on the current database adapter.
Constructor Details
#initialize(model_class) ⇒ Factory
Use for_model instead of instatiating a record directly.
35 36 37 38 |
# File 'lib/populator/factory.rb', line 35 def initialize(model_class) @model_class = model_class @records = [] end |
Class Method Details
.for_model(model_class) ⇒ Object
Fetches the factory dedicated to a given model class. You should always use this method instead of instatiating a factory directly so that a single factory is shared on multiple calls.
12 13 14 |
# File 'lib/populator/factory.rb', line 12 def self.for_model(model_class) @factories[model_class] ||= new(model_class) end |
.remember_depth ⇒ Object
Keep track of nested factory calls so we can save the remaining records once we are done with the base factory. This makes Populator more efficient when nesting factories.
27 28 29 30 31 32 |
# File 'lib/populator/factory.rb', line 27 def self.remember_depth @depth += 1 yield @depth -= 1 save_remaining_records if @depth.zero? end |
.save_remaining_records ⇒ Object
Find all remaining factories and call save_records on them.
17 18 19 20 21 22 |
# File 'lib/populator/factory.rb', line 17 def self.save_remaining_records @factories.values.each do |factory| factory.save_records end @factories = {} end |
Instance Method Details
#build_records(amount, per_query, &block) ⇒ Object
Builds multiple Populator::Record instances and calls save_records them when :per_query limit option is reached.
49 50 51 52 53 54 55 56 |
# File 'lib/populator/factory.rb', line 49 def build_records(amount, per_query, &block) amount.times do record = Record.new(@model_class, last_id_in_database + @records.size + 1) @records << record block.call(record) if block save_records if @records.size >= per_query end end |
#populate(amount, options = {}, &block) ⇒ Object
Entry method for building records. Delegates to build_records after remember_depth.
41 42 43 44 45 |
# File 'lib/populator/factory.rb', line 41 def populate(amount, = {}, &block) self.class.remember_depth do build_records(Populator.interpret_value(amount), [:per_query] || DEFAULT_RECORDS_PER_QUERY, &block) end end |
#save_records ⇒ Object
Saves the records to the database by calling populate on the current database adapter.
59 60 61 62 63 64 65 |
# File 'lib/populator/factory.rb', line 59 def save_records unless @records.empty? @model_class.connection.populate(@model_class.quoted_table_name, columns_sql, rows_sql_arr, "#{@model_class.name} Populate") @last_id_in_database = @records.last.id @records.clear end end |