Class: CovLoupe::PathRelativizer
- Inherits:
-
Object
- Object
- CovLoupe::PathRelativizer
- Defined in:
- lib/cov_loupe/paths/path_relativizer.rb
Overview
Utility object that converts configured path-bearing keys to forms relative to a project root while leaving the original payload untouched.
Operates on a deep copy of the payload to avoid mutating the caller's data. Only keys listed in scalar_keys (single path) and array_keys (list of paths) are relativized; all other values pass through unchanged.
Example usage:
relativizer = PathRelativizer.new(root: "/project", scalar_keys: ["file"], array_keys: ["files"])
relativizer.relativize({ "file" => "/project/lib/foo.rb", "files" => ["/project/lib/bar.rb"] })
# => { "file" => "lib/foo.rb", "files" => ["lib/bar.rb"] }
Instance Method Summary collapse
-
#initialize(root:, scalar_keys:, array_keys: []) ⇒ PathRelativizer
constructor
A new instance of PathRelativizer.
- #relativize(obj) ⇒ Object
-
#relativize_path(path) ⇒ String
Converts an absolute path to a path relative to the root.
Constructor Details
#initialize(root:, scalar_keys:, array_keys: []) ⇒ PathRelativizer
Returns a new instance of PathRelativizer.
19 20 21 22 23 |
# File 'lib/cov_loupe/paths/path_relativizer.rb', line 19 def initialize(root:, scalar_keys:, array_keys: []) @root = Pathname.new(PathUtils.(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
25 26 27 |
# File 'lib/cov_loupe/paths/path_relativizer.rb', line 25 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).
34 35 36 37 |
# File 'lib/cov_loupe/paths/path_relativizer.rb', line 34 def relativize_path(path) # PathUtils handles all the complexity automatically PathUtils.relativize(path, @root.to_s) end |