Class: CovLoupe::AppContext
- Inherits:
-
Data
- Object
- Data
- CovLoupe::AppContext
- Defined in:
- lib/cov_loupe/config/app_context.rb
Overview
Immutable configuration context for a CovLoupe session.
Bundles error handling, logging, and mode into a single object that can be scoped to a thread (via CovLoupe.with_context) or used globally.
Implemented as a Data class (Ruby 3.2+) for immutability and structural equality.
The custom #with method ensures the derived logger field is regenerated when
log_target or mode changes, since the logger depends on both.
Instance Attribute Summary collapse
-
#app_config ⇒ Object
readonly
Returns the value of attribute app_config.
-
#error_handler ⇒ Object
readonly
Returns the value of attribute error_handler.
-
#log_target ⇒ Object
readonly
Returns the value of attribute log_target.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #cli_mode? ⇒ Boolean
-
#initialize(error_handler:, log_target: nil, mode: :library, app_config: nil, logger: nil) ⇒ AppContext
constructor
A new instance of AppContext.
- #library_mode? ⇒ Boolean
- #mcp_mode? ⇒ Boolean
-
#with(**kwargs) ⇒ Object
Overrides Data#with to handle derived state.
Constructor Details
#initialize(error_handler:, log_target: nil, mode: :library, app_config: nil, logger: nil) ⇒ AppContext
Returns a new instance of AppContext.
15 16 17 18 |
# File 'lib/cov_loupe/config/app_context.rb', line 15 def initialize(error_handler:, log_target: nil, mode: :library, app_config: nil, logger: nil) logger ||= Logger.new(target: log_target, mode: mode) super end |
Instance Attribute Details
#app_config ⇒ Object (readonly)
Returns the value of attribute app_config
14 15 16 |
# File 'lib/cov_loupe/config/app_context.rb', line 14 def app_config @app_config end |
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler
14 15 16 |
# File 'lib/cov_loupe/config/app_context.rb', line 14 def error_handler @error_handler end |
#log_target ⇒ Object (readonly)
Returns the value of attribute log_target
14 15 16 |
# File 'lib/cov_loupe/config/app_context.rb', line 14 def log_target @log_target end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger
14 15 16 |
# File 'lib/cov_loupe/config/app_context.rb', line 14 def logger @logger end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode
14 15 16 |
# File 'lib/cov_loupe/config/app_context.rb', line 14 def mode @mode end |
Instance Method Details
#cli_mode? ⇒ Boolean
42 43 44 |
# File 'lib/cov_loupe/config/app_context.rb', line 42 def cli_mode? mode == :cli end |
#library_mode? ⇒ Boolean
46 47 48 |
# File 'lib/cov_loupe/config/app_context.rb', line 46 def library_mode? mode == :library end |
#mcp_mode? ⇒ Boolean
38 39 40 |
# File 'lib/cov_loupe/config/app_context.rb', line 38 def mcp_mode? mode == :mcp end |
#with(**kwargs) ⇒ Object
Overrides Data#with to handle derived state.
Since the logger depends on log_target and mode, we must ensure
it is regenerated if either of those fields are changed. Otherwise,
the new instance would point to the old logger (e.g. logging to the
wrong file).
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cov_loupe/config/app_context.rb', line 26 def with(**kwargs) target_changed = kwargs.key?(:log_target) && kwargs[:log_target] != log_target mode_changed = kwargs.key?(:mode) && kwargs[:mode] != mode if target_changed || mode_changed target = kwargs.fetch(:log_target, log_target) new_mode = kwargs.fetch(:mode, mode) kwargs[:logger] = Logger.new(target: target, mode: new_mode) end super end |