Class: Synthra::Behaviors::RandomizeOrder

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/behaviors/randomize_order.rb

Overview

Randomize order behavior - shuffles arrays in result

Recursively shuffles all array elements in the generated result to simulate non-deterministic ordering.

Examples:

Apply randomize order behavior

behavior = RandomizeOrder.new(50, rng)  # 50% probability
result = behavior.apply({ "tags" => ["a", "b", "c"] })
# => { "tags" => ["c", "a", "b"] }  # Shuffled 50% of the time

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::Behaviors::Base

Instance Method Details

#apply(result, context = nil) ⇒ Object

Apply randomize order behavior to result

Recursively shuffles all arrays found in the result hash.

Parameters:

  • result (Object)

    generated data to modify

  • context (Generator::Context, nil) (defaults to: nil)

    generation context (not used)

Returns:

  • (Object)

    result with arrays shuffled



44
45
46
47
48
# File 'lib/synthra/behaviors/randomize_order.rb', line 44

def apply(result, context = nil)
  return result unless should_apply?

  shuffle_arrays(result)
end

#shuffle_arrays(value) ⇒ Object (private)

Recursively shuffle arrays in value

Traverses the value structure and shuffles all arrays found.

Parameters:

  • value (Object)

    value to process (Hash, Array, or other)

Returns:

  • (Object)

    value with arrays shuffled



61
62
63
64
65
66
67
68
69
70
# File 'lib/synthra/behaviors/randomize_order.rb', line 61

def shuffle_arrays(value)
  case value
  when Array
    value.map { |v| shuffle_arrays(v) }.shuffle
  when Hash
    value.transform_values { |v| shuffle_arrays(v) }
  else
    value
  end
end