Class: Synthra::Behaviors::Base Abstract
- Inherits:
-
Object
- Object
- Synthra::Behaviors::Base
- Defined in:
- lib/synthra/behaviors/base.rb
Overview
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.
Direct Known Subclasses
CloseConnection, Deprecated, Failure, Latency, PartialData, RandomizeOrder, Registry::BlockBehaviorInstance, SimulateError
Instance Attribute Summary collapse
-
#rng ⇒ Object
readonly
Returns the value of attribute rng.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#apply(result, context = nil) ⇒ Object
abstract
Apply the behavior to generated data.
-
#extract_probability ⇒ Integer?
private
Extract probability from the value configuration.
-
#initialize(value, rng = nil) ⇒ Base
constructor
Create a new behavior instance.
-
#should_apply? ⇒ Boolean
Check if this behavior should be applied.
Constructor Details
#initialize(value, rng = nil) ⇒ Base
Create a new behavior instance
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
#rng ⇒ Object (readonly)
Returns the value of attribute rng.
66 67 68 |
# File 'lib/synthra/behaviors/base.rb', line 66 def rng @rng end |
#value ⇒ Object (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
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.
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_probability ⇒ Integer? (private)
Extract probability from the value configuration
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.
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 |