Class: Textus::Application::Maintenance::KeyMvPrefix::Impl

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/application/maintenance/key_mv_prefix.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, caps:, session:) ⇒ Impl

Returns a new instance of Impl.



12
13
14
15
16
# File 'lib/textus/application/maintenance/key_mv_prefix.rb', line 12

def initialize(ctx:, caps:, session:)
  @ctx     = ctx
  @caps    = caps
  @session = session
end

Instance Method Details

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

Raises:



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

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)
  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|
    Textus::Application::Write::Mv.call(
      s["from"], s["to"],
      session: @session, ctx: @ctx, caps: @session.write_caps,
      dry_run: false
    )
  end
  plan
end