Class: Typelizer::WriterContext
- Inherits:
-
Object
- Object
- Typelizer::WriterContext
- Defined in:
- lib/typelizer/contexts/writer_context.rb
Overview
Context for a single writer during a generation pass.
-
Caches one Interface per serializer class (prevents duplicates/loops)
-
Computes per-serializer effective Config: library defaults < global (flat setters) < writer < DSL (parent → child)
Instance Attribute Summary collapse
-
#writer_config ⇒ Object
readonly
Returns the value of attribute writer_config.
-
#writer_name ⇒ Object
readonly
Returns the value of attribute writer_name.
Instance Method Summary collapse
-
#config_for(serializer_class) ⇒ Object
Resolves the effective configuration for a serializer class by merging configuration layers in priority order: Library defaults Global configuration settings Writer-specific configuration DSL configuration with inheritance (highest priority).
-
#initialize(writer_name: nil, configuration: Typelizer.configuration) ⇒ WriterContext
constructor
A new instance of WriterContext.
-
#interface_for(serializer_class) ⇒ Object
Returns a memoized Interface for the given serializer class within this writer context Guarantees a single Interface instance per serializer (in this context), which: - preserves object identity across associations, - prevents infinite loops on cyclic relations, - and avoids redundant recomputation The cache is scoped to WriterContext (i.e., per writer and per generation run).
Constructor Details
#initialize(writer_name: nil, configuration: Typelizer.configuration) ⇒ WriterContext
Returns a new instance of WriterContext.
11 12 13 14 15 16 17 18 19 |
# File 'lib/typelizer/contexts/writer_context.rb', line 11 def initialize(writer_name: nil, configuration: Typelizer.configuration) @configuration = configuration @writer_name = (writer_name || Configuration::DEFAULT_WRITER_NAME).to_sym @writer_config = configuration.writer_config(@writer_name) @interface_cache = {} @config_cache = {} @dsl_cache = {} end |
Instance Attribute Details
#writer_config ⇒ Object (readonly)
Returns the value of attribute writer_config.
9 10 11 |
# File 'lib/typelizer/contexts/writer_context.rb', line 9 def writer_config @writer_config end |
#writer_name ⇒ Object (readonly)
Returns the value of attribute writer_name.
9 10 11 |
# File 'lib/typelizer/contexts/writer_context.rb', line 9 def writer_name @writer_name end |
Instance Method Details
#config_for(serializer_class) ⇒ Object
Resolves the effective configuration for a serializer class by merging configuration layers in priority order:
Library defaults
Global configuration settings
Writer-specific configuration
DSL configuration with inheritance (highest priority)
42 43 44 45 46 |
# File 'lib/typelizer/contexts/writer_context.rb', line 42 def config_for(serializer_class) raise ArgumentError, "Serializer class cannot be nil" unless serializer_class @config_cache[serializer_class] ||= build_config(serializer_class) end |
#interface_for(serializer_class) ⇒ Object
Returns a memoized Interface for the given serializer class within this writer context Guarantees a single Interface instance per serializer (in this context), which:
-
preserves object identity across associations,
-
prevents infinite loops on cyclic relations,
-
and avoids redundant recomputation
The cache is scoped to WriterContext (i.e., per writer and per generation run)
27 28 29 30 31 32 33 34 |
# File 'lib/typelizer/contexts/writer_context.rb', line 27 def interface_for(serializer_class) raise ArgumentError, "Serializer class cannot be nil" if serializer_class.nil? @interface_cache[serializer_class] ||= Interface.new( serializer: serializer_class, context: self ) end |