Class: Rigor::SigGen::PathMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/sig_gen/path_mapper.rb

Overview

Maps a source ‘.rb` file to its target `.rbs` sig file under the project’s signature tree.

ADR-14 § “Output layout”:

  • ‘–write` MUST NOT touch files outside `configuration.signature_paths` (default `sig/`).

  • The first slice supports one source file → one RBS file; multi-class files emit one RBS containing both classes (handled by the Writer, not here).

The mapping convention mirrors the Ruby community default: strip the source root prefix (the first entry of ‘configuration.paths`, typically `“lib”`), swap the extension, and place the result under the first entry of `configuration.signature_paths` (typically `“sig”`).

ADR-14 follow-up: when a class is already declared in an existing consolidated sig file (e.g. ‘sig/rigor/type.rbs` holds all `Rigor::Type::*` classes), the optional `LayoutIndex` re-routes the target to that file so the writer updates the consolidated declaration instead of creating a duplicate at the 1:1 mirror path.

When the source path is not under any configured source root (e.g. files supplied directly on the CLI from outside ‘lib/`), the full relative path is preserved under the sig root.

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, project_root: Dir.pwd, layout_index: nil) ⇒ PathMapper

Returns a new instance of PathMapper.

Parameters:

  • configuration (Rigor::Configuration)
  • project_root (String, Pathname) (defaults to: Dir.pwd)

    (defaults to ‘Dir.pwd`)

  • layout_index (LayoutIndex, nil) (defaults to: nil)

    optional class → existing sig file index; routes the target to the consolidated file when the class is already declared.



38
39
40
41
42
# File 'lib/rigor/sig_gen/path_mapper.rb', line 38

def initialize(configuration:, project_root: Dir.pwd, layout_index: nil)
  @configuration = configuration
  @project_root = Pathname(project_root)
  @layout_index = layout_index
end

Instance Method Details

#existing_target_for(class_name) ⇒ Object



60
61
62
63
64
# File 'lib/rigor/sig_gen/path_mapper.rb', line 60

def existing_target_for(class_name)
  return nil if class_name.nil? || @layout_index.nil?

  @layout_index.file_for(class_name)
end

#sig_root_dirObject

The directory ‘–write` is allowed to create / modify. Used by callers to assert the target stays inside the configured signature tree before touching the disk.



69
70
71
# File 'lib/rigor/sig_gen/path_mapper.rb', line 69

def sig_root_dir
  @sig_root_dir ||= @project_root / sig_root_name
end

#target_for(source_path, class_name: nil) ⇒ Pathname

Returns absolute path of the target ‘.rbs` file for the candidate.

Parameters:

  • source_path (String)
  • class_name (String, nil) (defaults to: nil)

    fully-qualified Ruby class name. When supplied and matched by the ‘LayoutIndex`, the consolidated sig file’s path is returned instead of the 1:1 mirror.

Returns:

  • (Pathname)

    absolute path of the target ‘.rbs` file for the candidate.



51
52
53
54
55
56
57
58
# File 'lib/rigor/sig_gen/path_mapper.rb', line 51

def target_for(source_path, class_name: nil)
  existing = existing_target_for(class_name)
  return existing if existing

  rel_to_root = source_relative_to_root(source_path)
  stripped = strip_source_root(rel_to_root)
  sig_root_dir / "#{stripped.sub_ext('')}.rbs"
end