Class: CovLoupe::AppContext

Inherits:
Data
  • Object
show all
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

Instance Method Summary collapse

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_configObject (readonly)

Returns the value of attribute app_config

Returns:

  • (Object)

    the current value of app_config



14
15
16
# File 'lib/cov_loupe/config/app_context.rb', line 14

def app_config
  @app_config
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler

Returns:

  • (Object)

    the current value of error_handler



14
15
16
# File 'lib/cov_loupe/config/app_context.rb', line 14

def error_handler
  @error_handler
end

#log_targetObject (readonly)

Returns the value of attribute log_target

Returns:

  • (Object)

    the current value of log_target



14
15
16
# File 'lib/cov_loupe/config/app_context.rb', line 14

def log_target
  @log_target
end

#loggerObject (readonly)

Returns the value of attribute logger

Returns:

  • (Object)

    the current value of logger



14
15
16
# File 'lib/cov_loupe/config/app_context.rb', line 14

def logger
  @logger
end

#modeObject (readonly)

Returns the value of attribute mode

Returns:

  • (Object)

    the current value of mode



14
15
16
# File 'lib/cov_loupe/config/app_context.rb', line 14

def mode
  @mode
end

Instance Method Details

#cli_mode?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cov_loupe/config/app_context.rb', line 42

def cli_mode?
  mode == :cli
end

#library_mode?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cov_loupe/config/app_context.rb', line 46

def library_mode?
  mode == :library
end

#mcp_mode?Boolean

Returns:

  • (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