Class: Textus::Maintenance::KeyMvPrefix
- Inherits:
-
Object
- Object
- Textus::Maintenance::KeyMvPrefix
- 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
- #call(from_prefix, to_prefix, dry_run: false) ⇒ Object
-
#initialize(container:, call:) ⇒ KeyMvPrefix
constructor
A new instance of KeyMvPrefix.
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.
19 20 21 22 |
# File 'lib/textus/maintenance/key_mv_prefix.rb', line 19 def initialize(container:, call:) @container = container @call = call end |
Instance Method Details
#call(from_prefix, to_prefix, dry_run: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/textus/maintenance/key_mv_prefix.rb', line 24 def call(from_prefix, to_prefix, dry_run: false) raise UsageError.new("from_prefix and to_prefix required") if from_prefix.nil? || to_prefix.nil? leaves = list_leaves_under(from_prefix) # When from_prefix is itself a leaf, `delete_prefix("#{from_prefix}.")` # finds no trailing dot to strip, so the tail keeps the whole key and the # move silently targets "to_prefix.<full-from_prefix>". Refuse it — a # single-key rename is `mv`'s job, not the bulk prefix verb's. if leaves.include?(from_prefix) raise UsageError.new("from_prefix '#{from_prefix}' is itself a leaf — use `mv` to rename a single key") end 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 |