Class: Textus::Action::KeyDeletePrefix

Inherits:
Base
  • Object
show all
Extended by:
Contract::DSL
Defined in:
lib/textus/action/key_delete_prefix.rb

Constant Summary collapse

BURN =
:sync

Instance Method Summary collapse

Methods included from Contract::DSL

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

Methods inherited from Base

#args, inherited

Constructor Details

#initialize(prefix:, dry_run: false) ⇒ KeyDeletePrefix

Returns a new instance of KeyDeletePrefix.



21
22
23
24
25
# File 'lib/textus/action/key_delete_prefix.rb', line 21

def initialize(prefix:, dry_run: false)
  super()
  @prefix = prefix
  @dry_run = dry_run
end

Instance Method Details

#call(container:, call:) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/textus/action/key_delete_prefix.rb', line 27

def call(container:, call:)
  raise UsageError.new("prefix required") if @prefix.nil? || @prefix.empty?

  leaves = Textus::Action::List.new(prefix: @prefix).call(container: container)
                               .map { |row| row.is_a?(Hash) ? (row["key"] || row[:key]) : row }

  warnings = leaves.empty? ? ["no keys under #{@prefix}"] : []
  steps = leaves.map { |key| { "op" => "delete", "key" => key } }

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

  steps.each do |step|
    Textus::Action::KeyDelete.new(key: step["key"]).call(container: container, call: call)
  end
  plan
end