Class: Synthra::Behaviors::Base Abstract

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

Overview

This class is abstract.

Subclass and implement #apply

Base class for all behavior implementations

All behaviors inherit from Base and implement the #apply method. The base class provides probability-based activation through

should_apply? method.

Examples:

Implement a simple behavior

class Logging < Base
  def apply(result, context = nil)
    return result unless should_apply?
    puts "Generated: #{result.inspect}"
    result
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, rng = nil) ⇒ Base

Create a new behavior instance

Examples:

Create with probability

Latency.new(100..500, rng)
Failure.new(10, rng)  # 10% probability

Parameters:

  • value (Object)

    behavior configuration (probability, range, etc.)

  • rng (Generator::RNG, nil) (defaults to: nil)

    random number generator for determinism



79
80
81
82
# File 'lib/synthra/behaviors/base.rb', line 79

def initialize(value, rng = nil)
  @value = value
  @rng = rng
end

Instance Attribute Details

#rngObject (readonly)

Returns the value of attribute rng.



66
67
68
# File 'lib/synthra/behaviors/base.rb', line 66

def rng
  @rng
end

#valueObject (readonly)

Returns the value of attribute value.



59
60
61
# File 'lib/synthra/behaviors/base.rb', line 59

def value
  @value
end

Instance Method Details

#apply(result, context = nil) ⇒ Object

This method is abstract.

Must be implemented by subclasses

Apply the behavior to generated data

This is the main method that behaviors implement to modify generated data or produce side effects.

Examples:

Implementation

def apply(result, context = nil)
  return result unless should_apply?
  # ... modify result ...
  result
end

Parameters:

  • result (Object)

    the generated data to modify

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

    generation context

Returns:

  • (Object)

    the modified result

Raises:

  • (NotImplementedError)

    if not overridden



105
106
107
# File 'lib/synthra/behaviors/base.rb', line 105

def apply(result, context = nil)
  raise NotImplementedError, "#{self.class} must implement #apply"
end

#extract_probabilityInteger? (private)

Extract probability from the value configuration

Returns:

  • (Integer, nil)

    probability percentage (0-100) or nil



146
147
148
149
150
151
152
# File 'lib/synthra/behaviors/base.rb', line 146

def extract_probability
  case value
  when Integer then value
  when Hash then value[:probability] || value[:percent]
  else nil
  end
end

#should_apply?Boolean

Check if this behavior should be applied

Uses probability-based activation. If value is an Integer, it's treated as a percentage probability. If nil or >= 100, always applies.

Examples:

Probability check

behavior = Failure.new(10, rng)  # 10% probability
behavior.should_apply?  # => true (10% of the time)

Returns:

  • (Boolean)

    true if the behavior should be applied



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/synthra/behaviors/base.rb', line 123

def should_apply?
  probability = extract_probability

  # Always apply if no probability specified or 100%+
  return true if probability.nil? || probability >= 100


  # Use RNG for deterministic tests, or rand() otherwise
  if rng
    rng.rand(100) < probability
  else
    rand(100) < probability
  end
end