Class: Textus::Action::Accept

Inherits:
WriteVerb show all
Extended by:
Contract::DSL
Defined in:
lib/textus/action/accept.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(pending_key:) ⇒ Accept

Returns a new instance of Accept.



16
17
18
19
# File 'lib/textus/action/accept.rb', line 16

def initialize(pending_key:)
  super()
  @pending_key = pending_key
end

Instance Method Details

#call(container:, call:) ⇒ Object



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

def call(container:, call:)
  env = Textus::Action::Get.new(key: @pending_key).call(container: container, call: call)
  proposal = env.meta["proposal"] or raise Textus::ProposalError.new("entry has no proposal block: #{@pending_key}")
  target = proposal["target_key"] or raise Textus::ProposalError.new("proposal missing target_key")
  action = proposal["action"] || "put"

  case action
  when "put"
    Textus::Action::Put.new(
      key: target,
      meta: env.meta["_meta"] || {},
      body: env.body,
    ).call(container: container, call: call)
  when "delete"
    Textus::Action::KeyDelete.new(key: target).call(container: container, call: call)
  else
    raise Textus::ProposalError.new("unknown action: #{action}")
  end

  Textus::Action::KeyDelete.new(key: @pending_key).call(container: container, call: call)

  container.steps.publish(
    :proposal_accepted,
    ctx: Textus::Step::Context.for(container: container, call: call),
    key: @pending_key,
    target_key: target,
  )

  { "protocol" => Textus::PROTOCOL, "accepted" => @pending_key, "target_key" => target, "action" => action }
end