Class: Synthra::Behaviors::RandomizeOrder
- 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.
Instance Method Summary collapse
-
#apply(result, context = nil) ⇒ Object
Apply randomize order behavior to result.
-
#shuffle_arrays(value) ⇒ Object
private
Recursively shuffle arrays in value.
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.
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.
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 |