Class: Synthra::Generator::Context
- Inherits:
-
Object
- Object
- Synthra::Generator::Context
- 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.
Instance Attribute Summary collapse
-
#depth ⇒ Integer
Current recursion depth for nested schema generation.
-
#faker_adapter ⇒ FakerAdapter?
Faker adapter for deterministic fake data generation.
-
#parent_context ⇒ Context?
Parent context for nested schemas (enables copy() from parent).
-
#registry ⇒ Registry?
Registry for resolving cross-schema references.
-
#resolver ⇒ Resolver?
Resolver for cross-schema reference resolution.
-
#schema_name ⇒ Object
readonly
Returns the value of attribute schema_name.
-
#shared_context ⇒ Hash
Shared context for batch generation (values persist across records).
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Get value by key (hash-style access).
-
#[]=(key, value) ⇒ Object
Set value by key (hash-style access).
-
#each {|key, value| ... } ⇒ Enumerator
Iterate over key-value pairs.
-
#empty? ⇒ Boolean
Check if context is empty.
-
#get(path) ⇒ Object?
Get value with dot notation path support.
-
#has?(key) ⇒ Boolean
(also: #key?, #include?)
Check if a key exists.
-
#initialize(data = {}, registry: nil, depth: 0, parent_context: nil, shared_context: nil, resolver: nil, faker_adapter: nil) ⇒ Context
constructor
Create a new Context.
-
#keys ⇒ Array<String>
Get all keys.
-
#merge(other) ⇒ Context
Merge with another hash or context.
-
#method_missing(method, *args, &block) ⇒ Object
Dynamic method access (dot notation for fields).
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Check if method responds (for method_missing).
-
#set(key, value) ⇒ Context
Set value (chainable).
-
#size ⇒ Integer
(also: #length)
Get number of entries.
-
#to_h ⇒ Hash
(also: #to_hash)
Convert to a plain Hash.
-
#values ⇒ Array
Get all values.
-
#with_schema(name) ⇒ Context
Set the schema name for reference resolution.
Constructor Details
#initialize(data = {}, registry: nil, depth: 0, parent_context: nil, shared_context: nil, resolver: nil, faker_adapter: nil) ⇒ Context
Create a new Context
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.
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
#depth ⇒ Integer
Current recursion depth for nested schema generation
64 65 66 |
# File 'lib/synthra/generator/context.rb', line 64 def depth @depth end |
#faker_adapter ⇒ FakerAdapter?
Faker adapter for deterministic fake data generation
92 93 94 |
# File 'lib/synthra/generator/context.rb', line 92 def faker_adapter @faker_adapter end |
#parent_context ⇒ Context?
Parent context for nested schemas (enables copy() from parent)
71 72 73 |
# File 'lib/synthra/generator/context.rb', line 71 def parent_context @parent_context end |
#registry ⇒ Registry?
Registry for resolving cross-schema references
57 58 59 |
# File 'lib/synthra/generator/context.rb', line 57 def registry @registry end |
#resolver ⇒ Resolver?
Resolver for cross-schema reference resolution
85 86 87 |
# File 'lib/synthra/generator/context.rb', line 85 def resolver @resolver end |
#schema_name ⇒ Object (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_context ⇒ Hash
Shared context for batch generation (values persist across records)
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)
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)
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
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
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.
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
221 222 223 |
# File 'lib/synthra/generator/context.rb', line 221 def has?(key) @data.key?(key.to_s) end |
#keys ⇒ Array<String>
Get all 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.
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)
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)
173 174 175 176 |
# File 'lib/synthra/generator/context.rb', line 173 def set(key, value) self[key] = value self end |
#size ⇒ Integer Also known as: length
Get number of entries
365 366 367 |
# File 'lib/synthra/generator/context.rb', line 365 def size @data.size end |
#to_h ⇒ Hash Also known as: to_hash
Convert to a plain Hash
237 238 239 |
# File 'lib/synthra/generator/context.rb', line 237 def to_h @data.dup end |
#values ⇒ Array
Get all 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
267 268 269 270 |
# File 'lib/synthra/generator/context.rb', line 267 def with_schema(name) @schema_name = name self end |