Class: Synthra::Field
- Inherits:
-
Object
- Object
- Synthra::Field
- Defined in:
- lib/synthra/field.rb
Overview
Represents a single field in a schema definition
A Field encapsulates all information about a schema field including:
- Field name and whether it's optional
- Type name and arguments for generation
- Nullable status (can the value be nil?)
- Conditional expression (when should this field be included?)
- Field-level behaviors (latency, partial_data, etc.)
Instance Attribute Summary collapse
-
#behaviors ⇒ Object
readonly
Returns the value of attribute behaviors.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type_args ⇒ Object
readonly
Returns the value of attribute type_args.
-
#type_name ⇒ Object
readonly
Returns the value of attribute type_name.
Instance Method Summary collapse
-
#behavior(type) ⇒ FieldBehavior?
Get a specific behavior by type.
-
#conditional? ⇒ Boolean
Check if this field has a condition.
-
#has_behavior?(type) ⇒ Boolean
Check if this field has a specific behavior.
-
#initialize(name:, type_name:, type_args: {}, nullable: false, condition: nil, behaviors: []) ⇒ Field
constructor
Create a new Field instance.
-
#inspect ⇒ String
Inspect representation for debugging.
-
#modifiers ⇒ Hash
Get all modifiers as a hash.
-
#nullable? ⇒ Boolean
Check if this field's type is nullable.
-
#optional? ⇒ Boolean
Check if this field is optional.
-
#range ⇒ Range?
Get the range constraint if present.
-
#to_s ⇒ String
String representation of the field.
-
#unique? ⇒ Boolean
Check if this field has a uniqueness constraint.
Constructor Details
#initialize(name:, type_name:, type_args: {}, nullable: false, condition: nil, behaviors: []) ⇒ Field
Create a new Field instance
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/synthra/field.rb', line 117 def initialize(name:, type_name:, type_args: {}, nullable: false, condition: nil, behaviors: []) @raw_name = name @name = name.delete_suffix("?") @optional = name.end_with?("?") @type_name = type_name.to_s.delete_suffix("?") @type_args = type_args || {} @nullable = nullable || type_name.to_s.end_with?("?") @condition = condition @behaviors = behaviors end |
Instance Attribute Details
#behaviors ⇒ Object (readonly)
Returns the value of attribute behaviors.
87 88 89 |
# File 'lib/synthra/field.rb', line 87 def behaviors @behaviors end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
80 81 82 |
# File 'lib/synthra/field.rb', line 80 def condition @condition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
59 60 61 |
# File 'lib/synthra/field.rb', line 59 def name @name end |
#type_args ⇒ Object (readonly)
Returns the value of attribute type_args.
73 74 75 |
# File 'lib/synthra/field.rb', line 73 def type_args @type_args end |
#type_name ⇒ Object (readonly)
Returns the value of attribute type_name.
66 67 68 |
# File 'lib/synthra/field.rb', line 66 def type_name @type_name end |
Instance Method Details
#behavior(type) ⇒ FieldBehavior?
Get a specific behavior by type
240 241 242 |
# File 'lib/synthra/field.rb', line 240 def behavior(type) behaviors.find { |b| b.type == type.to_sym } end |
#conditional? ⇒ Boolean
Check if this field has a condition
Conditional fields are only included when another field has a truthy value. The condition is specified with "if field_name" in the DSL.
176 177 178 |
# File 'lib/synthra/field.rb', line 176 def conditional? !condition.nil? end |
#has_behavior?(type) ⇒ Boolean
Check if this field has a specific behavior
225 226 227 |
# File 'lib/synthra/field.rb', line 225 def has_behavior?(type) behaviors.any? { |b| b.type == type.to_sym } end |
#inspect ⇒ String
Inspect representation for debugging
293 294 295 |
# File 'lib/synthra/field.rb', line 293 def inspect "#<Field #{self}>" end |
#modifiers ⇒ Hash
Get all modifiers as a hash
Returns a hash containing all field modifiers (unique, range, nullable, optional) with their values, excluding nil values.
257 258 259 260 261 262 263 264 |
# File 'lib/synthra/field.rb', line 257 def modifiers { unique: unique?, range: range, nullable: nullable?, optional: optional? }.compact end |
#nullable? ⇒ Boolean
Check if this field's type is nullable
Nullable types (marked with ? suffix on type in DSL) can generate nil values. This is different from optional - nullable fields are always included but may have nil values.
159 160 161 |
# File 'lib/synthra/field.rb', line 159 def nullable? @nullable end |
#optional? ⇒ Boolean
Check if this field is optional
Optional fields (marked with ? suffix in DSL) may be omitted from the generated output entirely based on configuration or randomness.
141 142 143 |
# File 'lib/synthra/field.rb', line 141 def optional? @optional end |
#range ⇒ Range?
Get the range constraint if present
Range constraints limit the generated values to a specific range. Common for number types.
208 209 210 211 212 |
# File 'lib/synthra/field.rb', line 208 def range return nil unless type_args[:range] type_args[:range] end |
#to_s ⇒ String
String representation of the field
Returns a human-readable string showing the field definition similar to how it would appear in a DSL file.
279 280 281 282 283 284 285 |
# File 'lib/synthra/field.rb', line 279 def to_s parts = ["#{@raw_name}: #{type_name}"] parts << "(#{type_args})" unless type_args.empty? parts << "if #{condition}" if condition behaviors.each { |b| parts << "@#{b.type} #{b.value}" } parts.join(" ") end |
#unique? ⇒ Boolean
Check if this field has a uniqueness constraint
Unique fields will not generate duplicate values within a batch. The generator will retry up to max_unique_retries times.
192 193 194 |
# File 'lib/synthra/field.rb', line 192 def unique? type_args[:unique] == true end |