Class: SimpleCovMcp::PathRelativizer

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov_mcp/path_relativizer.rb

Overview

Utility object that converts configured path-bearing keys to forms relative to the project root while leaving the original payload untouched.

Instance Method Summary collapse

Constructor Details

#initialize(root:, scalar_keys:, array_keys: []) ⇒ PathRelativizer

Returns a new instance of PathRelativizer.



9
10
11
12
13
# File 'lib/simplecov_mcp/path_relativizer.rb', line 9

def initialize(root:, scalar_keys:, array_keys: [])
  @root = Pathname.new(File.absolute_path(root || '.'))
  @scalar_keys = Array(scalar_keys).map(&:to_s).freeze
  @array_keys = Array(array_keys).map(&:to_s).freeze
end

Instance Method Details

#relativize(obj) ⇒ Object



15
16
17
# File 'lib/simplecov_mcp/path_relativizer.rb', line 15

def relativize(obj)
  deep_copy_and_relativize(obj)
end

#relativize_path(path) ⇒ String

Converts an absolute path to a path relative to the root. Falls back to the original path if conversion fails (e.g., different drive on Windows).

Parameters:

  • path (String)

    file path (absolute or relative)

Returns:

  • (String)

    relative path or original path on failure



24
25
26
27
28
29
30
31
32
# File 'lib/simplecov_mcp/path_relativizer.rb', line 24

def relativize_path(path)
  root_str = @root.to_s
  abs = File.absolute_path(path, root_str)
  return path unless abs.start_with?(root_prefix(root_str)) || abs == root_str

  Pathname.new(abs).relative_path_from(@root).to_s
rescue ArgumentError
  path
end