Class: Synthra::FieldBehavior
- Inherits:
-
Object
- Object
- Synthra::FieldBehavior
- Defined in:
- lib/synthra/field.rb
Overview
Represents a field-level behavior
Field behaviors modify how individual fields are generated or returned. They can add latency, omit data, or apply other transformations to specific fields rather than the entire schema.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality with another FieldBehavior.
-
#initialize(type:, value:) ⇒ FieldBehavior
constructor
Create a new FieldBehavior.
-
#inspect ⇒ String
Inspect representation.
-
#percentage ⇒ Integer
Get the behavior value as a percentage.
-
#range ⇒ Range
Get the behavior value as a range.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(type:, value:) ⇒ FieldBehavior
Create a new FieldBehavior
345 346 347 348 |
# File 'lib/synthra/field.rb', line 345 def initialize(type:, value:) @type = type.to_sym @value = value end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
323 324 325 |
# File 'lib/synthra/field.rb', line 323 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
330 331 332 |
# File 'lib/synthra/field.rb', line 330 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Check equality with another FieldBehavior
Two behaviors are equal if they have the same type and value.
434 435 436 437 438 |
# File 'lib/synthra/field.rb', line 434 def ==(other) return false unless other.is_a?(FieldBehavior) type == other.type && value == other.value end |
#inspect ⇒ String
Inspect representation
421 422 423 |
# File 'lib/synthra/field.rb', line 421 def inspect "#<FieldBehavior #{self}>" end |
#percentage ⇒ Integer
Get the behavior value as a percentage
For probability-based behaviors, extracts the percentage value from various value formats.
366 367 368 369 370 371 372 |
# File 'lib/synthra/field.rb', line 366 def percentage case value when Integer then value when Hash then value[:percent] || value[:probability] || 0 else 0 end end |
#range ⇒ Range
Get the behavior value as a range
For range-based behaviors (like latency), converts various value formats into a Range object.
393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/synthra/field.rb', line 393 def range case value when Range then value when Hash min = value[:min] || 0 max = value[:max] || min min..max else value..value end end |
#to_s ⇒ String
String representation
411 412 413 |
# File 'lib/synthra/field.rb', line 411 def to_s "@#{type} #{value}" end |