Class: Synthra::Behaviors::PartialData
- Defined in:
- lib/synthra/behaviors/partial_data.rb
Overview
Partial data behavior - removes some fields
Randomly removes fields from generated records to simulate incomplete data. Prefers removing non-critical fields (excludes "id" and "uuid").
Instance Method Summary collapse
-
#apply(result, context = nil) ⇒ Hash
Apply partial data behavior to result.
Constructor Details
This class inherits a constructor from Synthra::Behaviors::Base
Instance Method Details
#apply(result, context = nil) ⇒ Hash
Apply partial data behavior to result
Removes a random subset of fields (approximately 30% of fields) from the result hash. Preserves critical fields like "id" and "uuid".
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/synthra/behaviors/partial_data.rb', line 54 def apply(result, context = nil) return result unless should_apply? return result unless result.is_a?(Hash) # Remove optional fields first keys = result.keys.select do |k| # Prefer removing fields that look optional !%w[id uuid].include?(k.to_s) end return result if keys.empty? # Remove a random subset using RNG for determinism count_to_remove = (keys.length * 0.3).ceil if @rng to_remove = keys.shuffle(random: @rng.instance_variable_get(:@random)).take(count_to_remove) else to_remove = keys.sample(count_to_remove) end to_remove.each { |k| result.delete(k) } result end |