Module: Polyrun::Data::Fixtures
- Defined in:
- lib/polyrun/data/fixtures.rb
Overview
Declarative YAML fixture batches (**YAML → table → rows**). Polyrun does not ship a seed/register loader DSL—only **stdlib YAML** + iteration helpers. Typical layout: spec/fixtures/polyrun/*.yml with top-level keys = table names.
users:
- name: Ada
email: ada@example.com
Class Method Summary collapse
-
.apply_insert_all!(batch, connection: nil) ⇒ Object
Bulk insert YAML rows via ActiveRecord (batch load optimization).
-
.each_table(batch) ⇒ Object
Iterates each table in a single batch hash.
-
.each_table_in_directory(dir) ⇒ Object
Loads all batches from
dirand yields (batch_name, table, rows). -
.load_directory(dir) ⇒ Object
Returns { “batch_name” => { “table” => [rows] } } for every
.ymlunderdir(recursive). - .load_yaml(path) ⇒ Object
Class Method Details
.apply_insert_all!(batch, connection: nil) ⇒ Object
Bulk insert YAML rows via ActiveRecord (batch load optimization). Requires ActiveRecord and a connection that responds to insert_all(table_name, records) (Rails 6+).
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/polyrun/data/fixtures.rb', line 54 def apply_insert_all!(batch, connection: nil) unless defined?(ActiveRecord::Base) raise Polyrun::Error, "Fixtures.apply_insert_all! requires ActiveRecord" end conn = connection || ActiveRecord::Base.connection each_table(batch) do |table, rows| next if rows.empty? conn.insert_all(table, rows) end end |
.each_table(batch) ⇒ Object
Iterates each table in a single batch hash. Skips keys starting with “_”.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/polyrun/data/fixtures.rb', line 28 def each_table(batch) return enum_for(:each_table, batch) unless block_given? batch.each do |table, rows| t = table.to_s next if t.start_with?("_") raise Polyrun::Error, "fixtures: #{t} must be an Array of rows" unless rows.is_a?(Array) yield(t, rows) end end |
.each_table_in_directory(dir) ⇒ Object
Loads all batches from dir and yields (batch_name, table, rows).
42 43 44 45 46 47 48 49 50 |
# File 'lib/polyrun/data/fixtures.rb', line 42 def each_table_in_directory(dir) return enum_for(:each_table_in_directory, dir) unless block_given? load_directory(dir).each do |batch_name, batch| each_table(batch) do |table, rows| yield(batch_name, table, rows) end end end |
.load_directory(dir) ⇒ Object
Returns { “batch_name” => { “table” => [rows] } } for every .yml under dir (recursive).
20 21 22 23 24 25 |
# File 'lib/polyrun/data/fixtures.rb', line 20 def load_directory(dir) Dir.glob(File.join(dir, "**", "*.yml")).sort.each_with_object({}) do |path, acc| key = File.basename(path, ".*") acc[key] = load_yaml(path) end end |
.load_yaml(path) ⇒ Object
15 16 17 |
# File 'lib/polyrun/data/fixtures.rb', line 15 def load_yaml(path) YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true) || {} end |