Class: Ace::Retro::Molecules::RetroMover

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/retro/molecules/retro_mover.rb

Overview

Moves retro folders to different locations within the retros root directory. Delegates to SpecialFolderDetector for folder name normalization. Handles Errno::EXDEV for cross-filesystem moves.

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ RetroMover

Returns a new instance of RetroMover.

Parameters:

  • root_dir (String)

    Root directory for retros



14
15
16
# File 'lib/ace/retro/molecules/retro_mover.rb', line 14

def initialize(root_dir)
  @root_dir = root_dir
end

Instance Method Details

#move(retro, to:, date: nil) ⇒ String

Move a retro folder to a target location

Parameters:

  • retro (Retro)

    Retro to move

  • to (String)

    Target folder name (short or full, e.g., “archive”, “_archive”)

  • date (Time, nil) (defaults to: nil)

    Date used to compute archive partition (default: Time.now)

Returns:

  • (String)

    New path of the retro directory



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ace/retro/molecules/retro_mover.rb', line 23

def move(retro, to:, date: nil)
  normalized = Ace::Support::Items::Atoms::SpecialFolderDetector.normalize(to)

  target_parent = if normalized == "_archive"
    partition = Ace::Support::Items::Atoms::DatePartitionPath.compute(date || Time.now)
    File.expand_path(File.join(@root_dir, normalized, partition))
  else
    File.expand_path(File.join(@root_dir, normalized))
  end

  root_real = File.expand_path(@root_dir)
  unless target_parent.start_with?(root_real + File::SEPARATOR)
    raise ArgumentError, "Path traversal detected in --to option"
  end
  FileUtils.mkdir_p(target_parent)

  folder_name = File.basename(retro.path)
  new_path = File.join(target_parent, folder_name)

  # Same-location no-op check
  return retro.path if File.expand_path(retro.path) == File.expand_path(new_path)

  atomic_move(retro.path, new_path)
end

#move_to_root(retro) ⇒ String

Move a retro to root (remove from special folder)

Parameters:

  • retro (Retro)

    Retro to move

Returns:

  • (String)

    New path of the retro directory



51
52
53
54
55
56
57
58
59
# File 'lib/ace/retro/molecules/retro_mover.rb', line 51

def move_to_root(retro)
  folder_name = File.basename(retro.path)
  new_path = File.join(@root_dir, folder_name)

  # Same-location no-op check
  return retro.path if File.expand_path(retro.path) == File.expand_path(new_path)

  atomic_move(retro.path, new_path)
end