Class: Synthra::FieldBehavior

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

Examples:

DSL field with behavior

# User:
#   profile_image: url @partial_data 30%   # 30% chance of null
#   bio: text @latency 100..500ms          # 100-500ms delay

Create a field behavior programmatically

behavior = FieldBehavior.new(
  type: :latency,
  value: { min: 100, max: 500 }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, value:) ⇒ FieldBehavior

Create a new FieldBehavior

Examples:

Percentage-based behavior

FieldBehavior.new(type: :partial_data, value: 30)

Range-based behavior

FieldBehavior.new(type: :latency, value: { min: 100, max: 500 })

Parameters:

  • type (Symbol, String)

    behavior type (:partial_data, :latency, etc.)

  • value (Integer, Range, Hash)

    behavior configuration



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

#typeObject (readonly)

Returns the value of attribute type.



323
324
325
# File 'lib/synthra/field.rb', line 323

def type
  @type
end

#valueObject (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.

Parameters:

Returns:

  • (Boolean)

    true if equal



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

#inspectString

Inspect representation

Returns:

  • (String)

    inspect string



421
422
423
# File 'lib/synthra/field.rb', line 421

def inspect
  "#<FieldBehavior #{self}>"
end

#percentageInteger

Get the behavior value as a percentage

For probability-based behaviors, extracts the percentage value from various value formats.

Examples:

FieldBehavior.new(type: :partial_data, value: 30).percentage
# => 30

FieldBehavior.new(type: :partial_data, value: { probability: 25 }).percentage
# => 25

Returns:

  • (Integer)

    the percentage (0-100)



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

#rangeRange

Get the behavior value as a range

For range-based behaviors (like latency), converts various value formats into a Range object.

Examples:

FieldBehavior.new(type: :latency, value: 100..500).range
# => 100..500

FieldBehavior.new(type: :latency, value: { min: 100, max: 500 }).range
# => 100..500

FieldBehavior.new(type: :latency, value: 100).range
# => 100..100

Returns:

  • (Range)

    the range



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_sString

String representation

Returns:

  • (String)

    string like "@latency 100..500"



411
412
413
# File 'lib/synthra/field.rb', line 411

def to_s
  "@#{type} #{value}"
end