Class: Synthra::Behaviors::PartialData

Inherits:
Base
  • Object
show all
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").

Examples:

Apply partial data behavior

behavior = PartialData.new(30, rng)  # 30% probability
result = behavior.apply({ "id" => "1", "name" => "John", "email" => "..." })
# => { "id" => "1", "name" => "John" }  # email removed 30% 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) ⇒ 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".

Examples:

apply({ "id" => "1", "name" => "John", "email" => "..." }, nil)
# => { "id" => "1", "name" => "John" }  # email removed

Parameters:

  • result (Hash)

    generated record to modify

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

    generation context (not used)

Returns:

  • (Hash)

    result with some fields removed



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