Class: Textus::Action::KeyMvPrefix
- Extended by:
- Contract::DSL
- Defined in:
- lib/textus/action/key_mv_prefix.rb
Constant Summary collapse
- BURN =
:sync
Instance Method Summary collapse
- #call(container:, call:) ⇒ Object
-
#initialize(from_prefix:, to_prefix:, dry_run: false) ⇒ KeyMvPrefix
constructor
A new instance of KeyMvPrefix.
Methods included from Contract::DSL
arg, around, cli, cli_stdin, contract, contract?, summary, surfaces, verb, view
Methods inherited from Base
Constructor Details
#initialize(from_prefix:, to_prefix:, dry_run: false) ⇒ KeyMvPrefix
Returns a new instance of KeyMvPrefix.
23 24 25 26 27 28 |
# File 'lib/textus/action/key_mv_prefix.rb', line 23 def initialize(from_prefix:, to_prefix:, dry_run: false) super() @from_prefix = from_prefix @to_prefix = to_prefix @dry_run = dry_run end |
Instance Method Details
#call(container:, call:) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/textus/action/key_mv_prefix.rb', line 30 def call(container:, call:) raise UsageError.new("from_prefix and to_prefix required") if @from_prefix.nil? || @to_prefix.nil? leaves = Textus::Action::List.new(prefix: @from_prefix).call(container: container) .map { |row| row.is_a?(Hash) ? (row["key"] || row[:key]) : row } 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 = Textus::Background::Plan.new(steps: steps, warnings: warnings) return plan if @dry_run steps.each do |step| Textus::Action::KeyMv.new(old_key: step["from"], new_key: step["to"]).call(container: container, call: call) end plan end |