Class: Synthra::Generator::Streamer

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/generator/streamer.rb

Overview

Streaming generator for large batches

Instance Method Summary collapse

Constructor Details

#initialize(schema, registry: nil, **options) ⇒ Streamer

Returns a new instance of Streamer.



8
9
10
11
12
# File 'lib/synthra/generator/streamer.rb', line 8

def initialize(schema, registry: nil, **options)
  @schema = schema
  @registry = registry
  @options = options
end

Instance Method Details

#generate_stream(count:) ⇒ Enumerator

Generate records as a stream

Returns an Enumerator that generates records on-demand. The Engine instance is reused across all records in the stream for efficiency.

Examples:

Stream processing

streamer = Streamer.new(schema, seed: 42)
streamer.generate_stream(count: 1000).each do |record|
  process(record)  # Records generated one at a time
end

Parameters:

  • count (Integer)

    number of records

Returns:

  • (Enumerator)

    lazy enumerator of records



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
# File 'lib/synthra/generator/streamer.rb', line 30

def generate_stream(count:)
  seed = @options[:seed]
  mode = @options[:mode] || :random
  overrides = @options[:overrides] || {}

  Enumerator.new(count) do |yielder|

    # Reuse single Engine instance for efficiency
    engine = Engine.new(@schema, registry: @registry)
    

    # Initialize RNG once with seed if provided
    # This ensures deterministic streaming - all records use the same RNG sequence
    if seed

      # Initialize RNG and FakerAdapter without generating a record
      rng = Synthra::Generator::RNG.new(seed)
      engine.instance_variable_set(:@rng, rng)
      engine.instance_variable_set(:@faker_adapter, 
        Synthra::Generator::FakerAdapter.new(rng))
    end
    
    count.times do

      # Continue RNG sequence without resetting (seed: nil)
      record = engine.generate(seed: nil, mode: mode, overrides: overrides)
      yielder << record
    end
  end
end