Module: Trailblazer::Endpoint::Protocol::Controller

Defined in:
lib/trailblazer/endpoint/protocol/controller.rb,
lib/trailblazer/endpoint/protocol/cipher.rb

Overview

Deserialize incoming state. Serialize outgoing state. What else?

Defined Under Namespace

Modules: Cipher

Class Method Summary collapse

Class Method Details

.copy_suspend_data_to_endpoint_ctx(ctx, domain_ctx:) ⇒ Object



33
34
35
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 33

def copy_suspend_data_to_endpoint_ctx(ctx, domain_ctx:, **)
  ctx[:suspend_data] = domain_ctx[:suspend_data] # FIXME: use {#insert_copy_from_domain_ctx!}
end

.decrypt?(ctx, encrypted_resume_data:) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 9

def decrypt?(ctx, encrypted_resume_data:, **)
  encrypted_resume_data
end

.deserialize_process_model?(ctx, process_model_from_resume_data:) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 17

def deserialize_process_model?(ctx, process_model_from_resume_data:, **)
  process_model_from_resume_data
end

.deserialize_process_model_id(ctx, resume_data:) ⇒ Object



21
22
23
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 21

def deserialize_process_model_id(ctx, resume_data:, **)
  ctx[:process_model_id] = resume_data["id"] # DISCUSS: overriding {:process_model_id}?
end

.deserialize_process_model_id_from_resume_data(ctx, resume_data:) ⇒ Object

FIXME: use Model() mechanics.



38
39
40
41
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 38

def deserialize_process_model_id_from_resume_data(ctx, resume_data:, **)
  # DISCUSS: should we warn when overriding an existing {process_model_id}?
  ctx[:process_model_id] = resume_data["id"] # DISCUSS: overriding {:process_model_id}? # FIXME: stolen from Advance___::Controller
end

.deserialize_resume_data(ctx, decrypted_value:) ⇒ Object



13
14
15
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 13

def deserialize_resume_data(ctx, decrypted_value:, **)
  ctx[:resume_data] = JSON.parse(decrypted_value)
end

.encrypt?(ctx, domain_ctx:) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 25

def encrypt?(ctx, domain_ctx:, **)
  ctx[:suspend_data] = domain_ctx[:suspend_data]
end

.insert_copy_from_domain_ctx!(protocol, variables, after: :domain_activity) ⇒ Object

FIXME: ‘:after` untested!



92
93
94
95
96
97
98
99
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 92

def insert_copy_from_domain_ctx!(protocol, variables, after: :domain_activity) # FIXME: `:after` untested!
  variables.each do |domain_name, endpoint_name|
    protocol.module_eval do
      pass ->(ctx, domain_ctx:, **) { ctx[endpoint_name] = domain_ctx[domain_name] if domain_ctx.key?(domain_name) },
        id: :"copy_[#{endpoint_name.inspect}]_from_domain_ctx[#{domain_name.inspect}]", after: after
    end
  end
end

.insert_copy_to_domain_ctx!(protocol, variables, before: :domain_activity) ⇒ Object

FIXME: ‘:before` untested!



83
84
85
86
87
88
89
90
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 83

def insert_copy_to_domain_ctx!(protocol, variables, before: :domain_activity) # FIXME: `:before` untested!
  variables.each do |original_name, domain_name|
    protocol.module_eval do
      pass ->(ctx, domain_ctx:, **) { domain_ctx[domain_name] = ctx[original_name] if ctx.key?(original_name) },
        id: :"copy_[#{original_name.inspect}]_to_domain_ctx[#{domain_name.inspect}]", before: before
    end
  end
end

.insert_deserialize_steps!(activity, deserialize_before: :policy) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 43

def insert_deserialize_steps!(activity, deserialize_before: :policy)
  activity.module_eval do
    step Controller.method(:decrypt?), id: :decrypt?, before: deserialize_before # error out if no serialized_resume_data given.
    step Controller::Cipher.method(:decrypt_value), id: :decrypt,
        input: {cipher_key: :cipher_key, encrypted_resume_data: :encrypted_value}    , before: deserialize_before,
        # Output(:failure) => Track(:success),
        Output(:success) => Path(connect_to: Track(:success), track_color: :deserialize, before: deserialize_before) do # usually, Path goes into {policy}

      step Controller.method(:deserialize_resume_data), id: :deserialize_resume_data
      # DISCUSS: unmarshall?
      # step Controller.method(:deserialize_process_model_id?), id: :deserialize_process_model_id?, activity.Output(Trailblazer::Activity::Left, :failure) => activity.Id(around_activity_id)
      # step Controller.method(:deserialize_process_model_id), id: :deserialize_process_model_id

      step ->(*) { true } # FIXME: otherwise we can't insert an element AFTER :deserialize_resume_data
    end
  end
end

.insert_find_process_model!(protocol, **options) ⇒ Object

Insert the “experimental” find_process_model steps



72
73
74
75
76
77
78
79
80
81
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 72

def insert_find_process_model!(protocol, **options)
  protocol.module_eval do
    step Subprocess(FindProcessModel), Output(:failure) => End(:not_found),
    id: :find_process_model,
    **options
      # after: :authenticate
  end

  insert_copy_to_domain_ctx!(protocol, {:process_model => :model})
end

.insert_serialize_steps!(activity, serialize_after: :domain_activity) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 61

def insert_serialize_steps!(activity, serialize_after: :domain_activity)
  activity.module_eval do
      # FIXME: reverse order for insertion
    step Controller::Cipher.method(:encrypt_value), id: :encrypt                                                , after: serialize_after,
        input: {cipher_key: :cipher_key, serialized_suspend_data: :value}, output: {encrypted_value: :encrypted_suspend_data}
    step Controller.method(:serialize_suspend_data), id: :serialize_suspend_data                                , after: serialize_after
    pass Controller.method(:copy_suspend_data_to_endpoint_ctx), id: :copy_suspend_data_to_endpoint_ctx          , after: serialize_after
  end
end

.serialize_suspend_data(ctx, suspend_data:) ⇒ Object



29
30
31
# File 'lib/trailblazer/endpoint/protocol/controller.rb', line 29

def serialize_suspend_data(ctx, suspend_data:, **)
  ctx[:serialized_suspend_data] = JSON.dump(suspend_data)
end