Class: Ace::Idea::Molecules::IdeaMover

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/idea/molecules/idea_mover.rb

Overview

Moves idea folders to different locations within the ideas root directory. Delegates to SpecialFolderDetector for folder name normalization.

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ IdeaMover

Returns a new instance of IdeaMover.

Parameters:

  • root_dir (String)

    Root directory for ideas



13
14
15
# File 'lib/ace/idea/molecules/idea_mover.rb', line 13

def initialize(root_dir)
  @root_dir = root_dir
end

Instance Method Details

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

Move an idea folder to a target location

Parameters:

  • idea (Idea)

    Idea to move

  • to (String)

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

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

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

Returns:

  • (String)

    New path of the idea directory



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

def move(idea, 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(idea.path)
  new_path = File.join(target_parent, folder_name)

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

  atomic_move(idea.path, new_path)
end

#move_to_root(idea) ⇒ String

Move an idea to root (remove from special folder)

Parameters:

  • idea (Idea)

    Idea to move

Returns:

  • (String)

    New path of the idea directory



50
51
52
53
54
55
56
57
58
# File 'lib/ace/idea/molecules/idea_mover.rb', line 50

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

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

  atomic_move(idea.path, new_path)
end