Class: Synthra::Generator::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/generator/context.rb

Overview

Generation context for accessing generated field values

Context acts as a hash-like container for field values during generation. It supports multiple access patterns (hash, dot notation) and nested path lookups.

Examples:

Create and use context

ctx = Context.new
ctx["name"] = "John"
ctx["name"]  # => "John"
ctx.name     # => "John" (dot notation)

Nested access

ctx["address"] = { "city" => "NYC" }
ctx.get("address.city")  # => "NYC"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, registry: nil, depth: 0, parent_context: nil, shared_context: nil, resolver: nil, faker_adapter: nil) ⇒ Context

Create a new Context

Examples:

Empty context

ctx = Context.new

Pre-populated context

ctx = Context.new({ name: "John", age: 30 })

With all options

ctx = Context.new(
  { name: "John" },
  registry: my_registry,
  depth: 1,
  parent_context: parent_ctx,
  shared_context: {}
)

Parameters:

  • data (Hash) (defaults to: {})

    initial data to populate the context

  • registry (Registry, nil) (defaults to: nil)

    registry for resolving references

  • depth (Integer) (defaults to: 0)

    current recursion depth

  • parent_context (Context, nil) (defaults to: nil)

    parent context for nested schemas

  • shared_context (Hash, nil) (defaults to: nil)

    shared context for batch generation

  • resolver (Resolver, nil) (defaults to: nil)

    resolver for cross-schema references

  • faker_adapter (FakerAdapter, nil) (defaults to: nil)

    adapter for Faker



121
122
123
124
125
126
127
128
129
130
# File 'lib/synthra/generator/context.rb', line 121

def initialize(data = {}, registry: nil, depth: 0, parent_context: nil, shared_context: nil, resolver: nil, faker_adapter: nil)
  @data = data.transform_keys(&:to_s)
  @schema_name = nil
  @registry = registry
  @depth = depth
  @parent_context = parent_context
  @shared_context = shared_context || {}
  @resolver = resolver
  @faker_adapter = faker_adapter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Dynamic method access (dot notation for fields)

Provides Ruby-style attribute access to context values. Nested hashes are automatically wrapped in Context objects.

Examples:

ctx.name       # same as ctx["name"]
ctx.name = "X" # same as ctx["name"] = "X"


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/synthra/generator/context.rb', line 283

def method_missing(method, *args, &block)
  method_name = method.to_s


  # Handle setters (method=)
  if method_name.end_with?("=")
    key = method_name.chomp("=")
    return self[key] = args.first
  end


  # Handle getters
  if @data.key?(method_name)
    value = @data[method_name]

    # Wrap nested hashes in Context for continued dot access
    return value.is_a?(Hash) ? Context.new(value) : value
  end

  super
end

Instance Attribute Details

#depthInteger

Current recursion depth for nested schema generation

Returns:

  • (Integer)

    the depth level



64
65
66
# File 'lib/synthra/generator/context.rb', line 64

def depth
  @depth
end

#faker_adapterFakerAdapter?

Faker adapter for deterministic fake data generation

Returns:



92
93
94
# File 'lib/synthra/generator/context.rb', line 92

def faker_adapter
  @faker_adapter
end

#parent_contextContext?

Parent context for nested schemas (enables copy() from parent)

Returns:

  • (Context, nil)

    the parent context



71
72
73
# File 'lib/synthra/generator/context.rb', line 71

def parent_context
  @parent_context
end

#registryRegistry?

Registry for resolving cross-schema references

Returns:

  • (Registry, nil)

    the schema registry



57
58
59
# File 'lib/synthra/generator/context.rb', line 57

def registry
  @registry
end

#resolverResolver?

Resolver for cross-schema reference resolution

Returns:

  • (Resolver, nil)

    the resolver instance



85
86
87
# File 'lib/synthra/generator/context.rb', line 85

def resolver
  @resolver
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



50
51
52
# File 'lib/synthra/generator/context.rb', line 50

def schema_name
  @schema_name
end

#shared_contextHash

Shared context for batch generation (values persist across records)

Returns:

  • (Hash)

    the shared context hash



78
79
80
# File 'lib/synthra/generator/context.rb', line 78

def shared_context
  @shared_context
end

Instance Method Details

#[](key) ⇒ Object?

Get value by key (hash-style access)

Examples:

ctx["name"]  # => "John"
ctx[:name]   # => "John" (symbols work too)

Parameters:

  • key (String, Symbol)

    the key to look up

Returns:

  • (Object, nil)

    the value or nil if not found



143
144
145
# File 'lib/synthra/generator/context.rb', line 143

def [](key)
  @data[key.to_s]
end

#[]=(key, value) ⇒ Object

Set value by key (hash-style access)

Examples:

ctx["name"] = "John"

Parameters:

  • key (String, Symbol)

    the key to set

  • value (Object)

    the value to store

Returns:

  • (Object)

    the value



158
159
160
# File 'lib/synthra/generator/context.rb', line 158

def []=(key, value)
  @data[key.to_s] = value
end

#each {|key, value| ... } ⇒ Enumerator

Iterate over key-value pairs

Yields:

  • (key, value)

    block for each pair

Returns:

  • (Enumerator)

    if no block given



345
346
347
# File 'lib/synthra/generator/context.rb', line 345

def each(&block)
  @data.each(&block)
end

#empty?Boolean

Check if context is empty

Returns:

  • (Boolean)

    true if no data



355
356
357
# File 'lib/synthra/generator/context.rb', line 355

def empty?
  @data.empty?
end

#get(path) ⇒ Object?

Get value with dot notation path support

Traverses nested hashes/contexts using dot-separated paths.

Examples:

ctx["user"] = { "address" => { "city" => "NYC" } }
ctx.get("user.address.city")  # => "NYC"

Parameters:

  • path (String)

    dot-separated path (e.g., "address.city")

Returns:

  • (Object, nil)

    the value or nil if not found



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/synthra/generator/context.rb', line 191

def get(path)
  parts = path.to_s.split(".")
  result = @data

  parts.each do |part|
    case result
    when Hash
      result = result[part] || result[part.to_sym]
    when Context
      result = result[part]
    else
      return nil
    end
    return nil if result.nil?
  end

  result
end

#has?(key) ⇒ Boolean Also known as: key?, include?

Check if a key exists

Examples:

ctx.has?("name")  # => true
ctx.has?("unknown")  # => false

Parameters:

  • key (String, Symbol)

    the key to check

Returns:

  • (Boolean)

    true if key exists



221
222
223
# File 'lib/synthra/generator/context.rb', line 221

def has?(key)
  @data.key?(key.to_s)
end

#keysArray<String>

Get all keys

Returns:

  • (Array<String>)

    list of keys



324
325
326
# File 'lib/synthra/generator/context.rb', line 324

def keys
  @data.keys
end

#merge(other) ⇒ Context

Merge with another hash or context

Creates a new Context with merged data. Does not modify original.

Examples:

new_ctx = ctx.merge({ email: "john@example.com" })

Parameters:

  • other (Hash, Context)

    values to merge in

Returns:

  • (Context)

    new context with merged data



255
256
257
258
# File 'lib/synthra/generator/context.rb', line 255

def merge(other)
  other_data = other.respond_to?(:to_h) ? other.to_h : other
  Context.new(@data.merge(other_data.transform_keys(&:to_s)))
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Check if method responds (for method_missing)

Parameters:

  • method (Symbol)

    method name

  • include_private (Boolean) (defaults to: false)

    include private methods

Returns:

  • (Boolean)

    true if method would be handled



313
314
315
316
# File 'lib/synthra/generator/context.rb', line 313

def respond_to_missing?(method, include_private = false)
  method_name = method.to_s.chomp("=")
  @data.key?(method_name) || super
end

#set(key, value) ⇒ Context

Set value (chainable)

Examples:

ctx.set(:name, "John").set(:age, 30)

Parameters:

  • key (String, Symbol)

    the key to set

  • value (Object)

    the value to store

Returns:



173
174
175
176
# File 'lib/synthra/generator/context.rb', line 173

def set(key, value)
  self[key] = value
  self
end

#sizeInteger Also known as: length

Get number of entries

Returns:

  • (Integer)

    number of key-value pairs



365
366
367
# File 'lib/synthra/generator/context.rb', line 365

def size
  @data.size
end

#to_hHash Also known as: to_hash

Convert to a plain Hash

Examples:

ctx.to_h  # => { "name" => "John", "age" => 30 }

Returns:

  • (Hash)

    copy of internal data



237
238
239
# File 'lib/synthra/generator/context.rb', line 237

def to_h
  @data.dup
end

#valuesArray

Get all values

Returns:

  • (Array)

    list of values



334
335
336
# File 'lib/synthra/generator/context.rb', line 334

def values
  @data.values
end

#with_schema(name) ⇒ Context

Set the schema name for reference resolution

Parameters:

  • name (String)

    the schema name

Returns:



267
268
269
270
# File 'lib/synthra/generator/context.rb', line 267

def with_schema(name)
  @schema_name = name
  self
end