Class: Textus::Handlers::Write::DataMv

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/handlers/write/data_mv.rb

Instance Method Summary collapse

Constructor Details

#initialize(container:) ⇒ DataMv

Returns a new instance of DataMv.



7
8
9
# File 'lib/textus/handlers/write/data_mv.rb', line 7

def initialize(container:)
  @container = container
end

Instance Method Details

#call(command, _call) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/textus/handlers/write/data_mv.rb', line 11

def call(command, _call)
  manifest = @container.manifest
  geom = @container.layout

  return Value::Result.failure(:usage_error, "from and to required") if command.from.nil? || command.to.nil?
  unless manifest.data.declared_lane_kinds.key?(command.from)
    return Value::Result.failure(:usage_error,
                                 "data lane '#{command.from}' not declared")
  end

  dest_dir = geom.lane_path(command.to)
  return Value::Result.failure(:usage_error, "destination 'data/#{command.to}' already exists") if File.exist?(dest_dir)

  affected_keys = manifest.data.entries.select { |entry| entry.lane == command.from }.map(&:key)

  steps = [{ "op" => "rename_zone", "from" => command.from, "to" => command.to }]
  steps += affected_keys.map do |key|
    { "op" => "mv", "from" => key, "to" => "#{command.to}#{key[command.from.length..]}" }
  end

  plan = Textus::Store::Jobs::Plan.new(steps: steps, warnings: [])
  return Value::Result.success(plan) if command.dry_run

  rewrite_manifest!(geom, from: command.from, to: command.to)
  FileUtils.mv(geom.lane_path(command.from), dest_dir)
  Value::Result.success(plan)
end