Class: Textus::Handlers::Write::MoveKey

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

Instance Method Summary collapse

Constructor Details

#initialize(container:, manifest:) ⇒ MoveKey

Returns a new instance of MoveKey.



5
6
7
8
# File 'lib/textus/handlers/write/move_key.rb', line 5

def initialize(container:, manifest:)
  @container = container
  @manifest = manifest
end

Instance Method Details

#call(command, call) ⇒ Object



10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/textus/handlers/write/move_key.rb', line 10

def call(command, call)
  Textus::Manifest::Data.validate_key!(command.old_key)
  Textus::Manifest::Data.validate_key!(command.new_key)

  return Value::Result.failure(:usage_error, "mv: old and new keys are identical") if command.old_key == command.new_key

  old_res = @manifest.resolver.resolve(command.old_key)
  new_res = @manifest.resolver.resolve(command.new_key)

  reader = Store::Entry::Reader.from(container: @container)

  unless reader.exists?(command.old_key)
    return Value::Result.failure(:not_found,
                                 "source key '#{command.old_key}' not found")
  end

  zone_check = validate_zone(old_res.entry, new_res.entry)
  return zone_check if zone_check

  if reader.exists?(command.new_key)
    return Value::Result.failure(:usage_error, "mv: target '#{command.new_key}' already exists at #{new_res.path}")
  end

  pre_env = reader.read(command.old_key)
  writer = Store::Entry::Writer.from(container: @container, call: call)
  unless pre_env.uid
    writer.put(
      command.old_key, mentry: old_res.entry,
                       payload: Textus::Value::Payload.new(meta: pre_env.meta, body: pre_env.body, content: pre_env.content)
    )
  end

  if command.dry_run
    return Value::Result.success({
                                   "protocol" => Textus::PROTOCOL, "ok" => true, "dry_run" => true,
                                   "from_key" => command.old_key, "to_key" => command.new_key,
                                   "from_path" => old_res.path, "to_path" => new_res.path,
                                   "uid" => pre_env.uid
                                 })
  end

  envelope = writer.move(
    from_key: command.old_key, to_key: command.new_key,
    new_mentry: new_res.entry
  )

  Value::Result.success({
                          "protocol" => Textus::PROTOCOL, "ok" => true,
                          "from_key" => command.old_key, "to_key" => command.new_key,
                          "from_path" => old_res.path, "to_path" => new_res.path,
                          "uid" => envelope.uid, "envelope" => envelope.to_h_for_wire
                        })
end