Class: Textus::Action::Accept

Inherits:
Base
  • Object
show all
Defined in:
lib/textus/action/accept.rb

Class Method Summary collapse

Methods inherited from Base

inherited, proposal_from

Methods included from Contract::DSL

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

Class Method Details

.call(container:, call:, pending_key:) ⇒ Object



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
# File 'lib/textus/action/accept.rb', line 12

def self.call(container:, call:, pending_key:)
  env = container.compositor.read(pending_key)
  parsed = proposal_from(env, key: pending_key)
  return parsed if parsed.is_a?(Dry::Monads::Result::Failure)

  target = parsed[:target_key]
  action = parsed[:proposal]["action"] || "put"

  case action
  when "put"
    mentry = container.manifest.resolver.resolve(target).entry
    container.compositor.write(
      target,
      mentry: mentry,
      payload: Textus::Store::Envelope::Writer::Payload.new(
        meta: env.meta["_meta"] || {},
        body: env.body,
        content: nil,
      ),
      call: call,
    )
  when "delete"
    container.compositor.delete(target, call: call)
  else
    return Failure(code: :proposal_error, message: "unknown action: #{action}")
  end

  container.compositor.delete(pending_key, call: call)

  Success("protocol" => Textus::PROTOCOL, "accepted" => pending_key, "target_key" => target, "action" => action,
          "cascade_key" => target)
end