Class: Textus::Maintenance::KeyMvPrefix

Inherits:
Object
  • Object
show all
Extended by:
Contract::DSL
Defined in:
lib/textus/maintenance/key_mv_prefix.rb

Overview

Bulk-rename every leaf key under ‘from_prefix` to `to_prefix`. Calls Write::Mv directly for each entry — emits one audit row per file moved.

Instance Method Summary collapse

Methods included from Contract::DSL

arg, around, cli, cli_stdin, contract, contract?, summary, surfaces, verb, view

Constructor Details

#initialize(container:, call:) ⇒ KeyMvPrefix

Returns a new instance of KeyMvPrefix.



18
19
20
21
# File 'lib/textus/maintenance/key_mv_prefix.rb', line 18

def initialize(container:, call:)
  @container    = container
  @call         = call
end

Instance Method Details

#call(from_prefix, to_prefix, dry_run: true) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/textus/maintenance/key_mv_prefix.rb', line 23

def call(from_prefix, to_prefix, dry_run: true)
  raise UsageError.new("from_prefix and to_prefix required") if from_prefix.nil? || to_prefix.nil?

  leaves = list_leaves_under(from_prefix)
  warnings = []
  warnings << "no keys under #{from_prefix}" if leaves.empty?

  steps = leaves.map do |old_key|
    tail = old_key.delete_prefix("#{from_prefix}.")
    new_key = "#{to_prefix}.#{tail}"
    { "op" => "mv", "from" => old_key, "to" => new_key }
  end

  plan = Plan.new(steps: steps, warnings: warnings)
  return plan if dry_run

  steps.each do |s|
    mv.call(s["from"], s["to"], dry_run: false)
  end
  plan
end