Class: Foobara::TransformedCommand

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.1.7/projects/command_connectors/src/transformed_command.rb

Overview

TODO: feels so strange that this doesn’t inherit from command

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(untransformed_inputs = {}) ⇒ TransformedCommand

Returns a new instance of TransformedCommand.



588
589
590
591
592
593
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 588

def initialize(untransformed_inputs = {})
  self.untransformed_inputs = untransformed_inputs || {}

  transform_inputs
  construct_command
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



841
842
843
844
845
846
847
848
849
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 841

def method_missing(method_name, ...)
  if command.respond_to?(method_name)
    command.send(method_name, ...)
  else
    # :nocov:
    super
    # :nocov:
  end
end

Class Attribute Details

.allowed_ruleObject

Returns the value of attribute allowed_rule.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def allowed_rule
  @allowed_rule
end

.authenticatorObject

Returns the value of attribute authenticator.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def authenticator
  @authenticator
end

.capture_unknown_errorObject

Returns the value of attribute capture_unknown_error.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def capture_unknown_error
  @capture_unknown_error
end

.command_classObject

Returns the value of attribute command_class.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def command_class
  @command_class
end

.command_nameObject

Returns the value of attribute command_name.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def command_name
  @command_name
end

.errors_transformersObject

Returns the value of attribute errors_transformers.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def errors_transformers
  @errors_transformers
end

.full_command_nameObject

Returns the value of attribute full_command_name.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def full_command_name
  @full_command_name
end

.inputs_transformersObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 109

def inputs_transformers
  return @inputs_transformers if @considered_inputs_sensitive_value_remover

  @considered_inputs_sensitive_value_remover = true

  inputs_type = command_class.inputs_type

  @inputs_transformers = transformers_to_processors(@inputs_transformers, inputs_type, direction: :to)
  @inputs_transformers = @inputs_transformers.reverse

  # TODO: this block looks pointless...
  @inputs_transformers.each do |transformer|
    if transformer.is_a?(TypeDeclarations::TypedTransformer)
      new_type = transformer.from_type
      if new_type
        inputs_type = new_type
        break
      end
    end
  end

  @inputs_transformers
end

.pre_commit_transformersObject

Returns the value of attribute pre_commit_transformers.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def pre_commit_transformers
  @pre_commit_transformers
end

.request_mutatorsObject

Returns the value of attribute request_mutators.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def request_mutators
  @request_mutators
end

.requires_authenticationObject

Returns the value of attribute requires_authentication.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def requires_authentication
  @requires_authentication
end

.response_mutatorsObject

Returns the value of attribute response_mutators.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def response_mutators
  @response_mutators
end

.result_transformersObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 70

def result_transformers
  return @result_transformers if @considered_sensitive_value_remover

  @considered_sensitive_value_remover = true

  result_type = command_class.result_type

  result_transformers.reverse.each do |transformer|
    if transformer.is_a?(TypeDeclarations::TypedTransformer) ||
       (transformer.is_a?(Class) && transformer < TypeDeclarations::TypedTransformer)
      new_type = transformer.to_type
      if new_type
        result_type = new_type
        break
      end
    end
  end

  if result_type&.has_sensitive_types?
    remover_class = Foobara::TypeDeclarations.sensitive_value_remover_class_for_type(result_type)

    remover = Namespace.use subclassed_in_namespace do
      path = if result_type.scoped_path_set?
               result_type.scoped_full_path
             else
               [*command_class.scoped_path, *suffix, "Result"]
             end

      remover_class.new(from: result_type).tap do |r|
        r.scoped_path = ["SensitiveValueRemover", *path]
      end
    end

    @result_transformers = [*@result_transformers, remover]
  end

  @result_transformers
end

.serializersObject

Returns the value of attribute serializers.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def serializers
  @serializers
end

.subclassed_in_namespaceObject

Returns the value of attribute subclassed_in_namespace.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def subclassed_in_namespace
  @subclassed_in_namespace
end

.suffixObject

Returns the value of attribute suffix.



7
8
9
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 7

def suffix
  @suffix
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



586
587
588
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 586

def command
  @command
end

#outcomeObject

Returns the value of attribute outcome.



586
587
588
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 586

def outcome
  @outcome
end

#requestObject

Returns the value of attribute request.



586
587
588
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 586

def request
  @request
end

#transformed_inputsObject

Returns the value of attribute transformed_inputs.



586
587
588
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 586

def transformed_inputs
  @transformed_inputs
end

#untransformed_inputsObject

Returns the value of attribute untransformed_inputs.



586
587
588
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 586

def untransformed_inputs
  @untransformed_inputs
end

Class Method Details

.descriptionObject



66
67
68
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 66

def description
  command_class.description
end

.error_context_type_mapObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 223

def error_context_type_map
  @error_context_type_map ||= begin
    set = {}

    command_class.possible_errors.each do |possible_error|
      # For now, we get the input errors off the transformed inputs_type.
      # This way if an inputs transformer changes the path of an input, we don't wind up with the old path
      # in the errors.
      if possible_error.error_class < Foobara::RuntimeError
        set[possible_error.key.to_s] = possible_error
      end
    end

    command_class.manually_added_possible_input_errors.each do |possible_error|
      set[possible_error.key.to_s] = possible_error
    end

    inputs_type&.possible_errors&.each do |possible_error|
      set[possible_error.key.to_s] = possible_error
    end

    errors_transformers.each do |transformer|
      set = transformer.transform_error_context_type_map(self, set)
    end

    set
  end
end

.foobara_manifestObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 336

def foobara_manifest
  to_include = TypeDeclarations.foobara_manifest_context_to_include || Set.new

  types = types_depended_on.select(&:registered?).map do |t|
    to_include << t
    t.foobara_manifest_reference
  end.sort

  inputs_transformers = TypeDeclarations.with_manifest_context(remove_sensitive: false) do
    processors_to_manifest_symbols(self.inputs_transformers)
  end
  result_transformers = processors_to_manifest_symbols(self.result_transformers)
  errors_transformers = processors_to_manifest_symbols(self.errors_transformers)
  pre_commit_transformers = processors_to_manifest_symbols(self.pre_commit_transformers)
  serializers = processors_to_manifest_symbols(self.serializers)
  response_mutators = processors_to_manifest_symbols(self.response_mutators)
  request_mutators = processors_to_manifest_symbols(self.request_mutators)

  authenticator_details = if requires_authentication && authenticator
                            {
                              symbol: authenticator.symbol,
                              explanation: authenticator.explanation
                            }
                          end

  inputs_types_depended_on = TypeDeclarations.with_manifest_context(remove_sensitive: false) do
    self.inputs_types_depended_on.map(&:foobara_manifest_reference).sort
  end

  result_types_depended_on = self.result_types_depended_on.map(&:foobara_manifest_reference)
  result_types_depended_on = result_types_depended_on.sort

  bit_bucket = Set.new
  manifest = TypeDeclarations.with_manifest_context(to_include: bit_bucket) do
    command_class.foobara_manifest
  end

  # TODO: This should support nil as an inputs_type but it breaks other projects for now
  inputs_type = inputs_type_for_manifest&.reference_or_declaration_data ||
                { type: :attributes, element_type_declarations: {} }

  # TODO: handle errors_types_depended_on!
  to_merge = {
    inputs_types_depended_on:,
    result_types_depended_on:,
    types_depended_on: types,
    inputs_type:,
    result_type: result_type&.reference_or_declaration_data,
    possible_errors: possible_errors_manifest,
    capture_unknown_error:,
    inputs_transformers:,
    result_transformers:,
    errors_transformers:,
    pre_commit_transformers:,
    serializers:,
    response_mutators:,
    request_mutators:,
    requires_authentication:,
    authenticator: authenticator_details
  }

  manifest = manifest.dup

  # TODO: we should remove all blank valued keys but keeping many for now for stability
  safe_to_remove = [
    :pre_commit_transformers,
    :response_mutators,
    :request_mutators,
    :capture_unknown_error,
    :inputs_transformers,
    :result_transformers,
    :errors_transformers,
    :requires_authentication,
    :authenticator
  ]

  to_merge.each_pair do |key, value|
    # TODO: we could probably remove empty strings and nils, too, and from the whole hash
    if value.nil? || ((value.is_a?(::Hash) || value.is_a?(::Array)) && value.empty?)
      if safe_to_remove.include?(key)
        manifest.delete(key)
      else
        manifest[key] = value
      end
    else
      manifest[key] = value
    end
  end

  manifest
end

.inputs_transformerObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 474

def inputs_transformer
  return @inputs_transformer if defined?(@inputs_transformer)

  transformers = inputs_transformers

  if transformers.empty?
    @inputs_transformer = nil
    return
  end

  @inputs_transformer = if transformers.size == 1
                          transformers.first
                        else
                          Value::Processor::Pipeline.new(processors: transformers)
                        end
end

.inputs_typeObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 205

def inputs_type
  return @inputs_type if defined?(@inputs_type)

  mutated_inputs_type = inputs_type_from_transformers

  mutators = if request_mutators.size == 1
               [request_mutator]
             else
               request_mutator&.processors
             end

  mutators&.each do |mutator|
    mutated_inputs_type = mutator.inputs_type_from(mutated_inputs_type)
  end

  @inputs_type = mutated_inputs_type
end

.inputs_type_for_manifestObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 133

def inputs_type_for_manifest
  return @inputs_type_for_manifest if defined?(@inputs_type_for_manifest)

  @inputs_type_for_manifest = if inputs_type&.has_sensitive_types?
                                remover_class = Foobara::TypeDeclarations.sensitive_value_remover_class_for_type(
                                  inputs_type
                                )

                                Namespace.use subclassed_in_namespace do
                                  remover_class.new(to: inputs_type).from_type
                                end
                              else
                                inputs_type
                              end
end

.inputs_type_from_transformersObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 149

def inputs_type_from_transformers
  return @inputs_type_from_transformers if defined?(@inputs_type_from_transformers)

  @inputs_type_from_transformers = if inputs_transformer
                                     if inputs_transformer.is_a?(Value::Processor::Pipeline)
                                       inputs_transformer.processors.each do |transformer|
                                         if transformer.is_a?(TypeDeclarations::TypedTransformer)
                                           from_type = transformer.from_type
                                           if from_type
                                             @inputs_type_from_transformers = from_type
                                             return from_type
                                           end
                                         end
                                       end

                                       command_class.inputs_type
                                     else
                                       if inputs_transformer.is_a?(TypeDeclarations::TypedTransformer)
                                         inputs_transformer.from_type
                                       end || command_class.inputs_type
                                     end
                                   else
                                     command_class.inputs_type
                                   end
end

.inputs_types_depended_onObject



270
271
272
273
274
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 270

def inputs_types_depended_on
  TypeDeclarations.with_manifest_context(remove_sensitive: false) do
    inputs_type&.types_depended_on || []
  end
end

.mutate_request(request) ⇒ Object



535
536
537
538
539
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 535

def mutate_request(request)
  if request_mutator&.applicable?(request)
    request_mutator.process_value!(request)
  end
end

.possible_errorsObject



252
253
254
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 252

def possible_errors
  @possible_errors ||= error_context_type_map.values
end

.possible_errors_manifestObject



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 256

def possible_errors_manifest
  errors_proc = -> do
    possible_errors.map do |possible_error|
      [possible_error.key.to_s, possible_error.foobara_manifest]
    end.sort.to_h
  end

  if TypeDeclarations.manifest_context_set?(:remove_sensitive)
    errors_proc.call
  else
    TypeDeclarations.with_manifest_context(remove_sensitive: true, &errors_proc)
  end
end

.processors_to_manifest_symbols(processors) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 428

def processors_to_manifest_symbols(processors)
  return nil if processors.nil? || processors.empty?

  to_include = TypeDeclarations.foobara_manifest_context_to_include || Set.new
  include_processors = TypeDeclarations.include_processors?

  processors.map do |processor|
    if processor.respond_to?(:scoped_path_set?) && processor.scoped_path_set?
      if include_processors
        to_include << processor
      end
      processor.foobara_manifest_reference
    elsif processor.is_a?(Value::Processor)
      klass = processor.class

      if klass.scoped_path_set?
        if include_processors
          to_include << klass
        end
        klass.foobara_manifest_reference
        # TODO: Delete this nocov block
        # TODO: make anonymous scoped path's have better names instead of random hexadecimal
        # :nocov:
      elsif processor.respond_to?(:symbol) && processor.symbol
        processor.symbol
      else
        name = klass.name

        while name.nil?
          klass = klass.superclass
          name = klass.name
        end

        "Anonymous#{Util.non_full_name(name)}"
        # :nocov:
      end
    elsif processor.is_a?(::Proc)
      "Proc"
    else
      # :nocov:
      "Unknown"
      # :nocov:
    end
  end
end

.request_mutatorObject



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 513

def request_mutator
  return @request_mutator if defined?(@request_mutator)

  # HACK: to give SensitiveValueRemover a chance to be injected
  inputs_transformer

  if request_mutators.empty?
    @request_mutator = nil
    return
  end

  @request_mutator = begin
    transformers = transformers_to_processors(request_mutators, inputs_type_from_transformers, direction: :to)

    if transformers.size == 1
      transformers.first
    else
      Value::Processor::Pipeline.new(processors: transformers)
    end
  end
end

.response_mutatorObject



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 491

def response_mutator
  return @response_mutator if defined?(@response_mutator)

  # A hack: this will give the SensitiveValueRemover a chance to be injected
  result_transformers

  if response_mutators.empty?
    @response_mutator = nil
    return
  end

  @response_mutator = begin
    transformers = transformers_to_processors(response_mutators, result_type_from_transformers, direction: :from)

    if transformers.size == 1
      transformers.first
    else
      Value::Processor::Pipeline.new(processors: transformers)
    end
  end
end

.result_transformerObject



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 541

def result_transformer
  return @result_transformer if defined?(@result_transformer)

  if result_transformers.empty?
    @result_transformer = nil
    return
  end

  @result_transformer = begin
    transformers = transformers_to_processors(result_transformers, command_class.result_type, direction: :from)

    if transformers.size == 1
      transformers.first
    else
      Value::Processor::Pipeline.new(processors: transformers)
    end
  end
end

.result_typeObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 187

def result_type
  return @result_type if defined?(@result_type)

  mutated_result_type = result_type_from_transformers

  mutators = if response_mutators.size == 1
               [response_mutator]
             else
               response_mutator&.processors&.reverse
             end

  mutators&.each do |mutator|
    mutated_result_type = mutator.result_type_from(mutated_result_type)
  end

  @result_type = mutated_result_type
end

.result_type_from_transformersObject



175
176
177
178
179
180
181
182
183
184
185
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 175

def result_type_from_transformers
  result_transformers.reverse.each do |transformer|
    if transformer.is_a?(TypeDeclarations::TypedTransformer) ||
       (transformer.is_a?(Class) && transformer < TypeDeclarations::TypedTransformer)
      new_type = transformer.to_type
      return new_type if new_type
    end
  end

  command_class.result_type
end

.result_types_depended_onObject



276
277
278
279
280
281
282
283
284
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 276

def result_types_depended_on
  type_proc = -> { result_type&.types_depended_on || [] }

  if TypeDeclarations.manifest_context_set?(:remove_sensitive)
    type_proc.call
  else
    TypeDeclarations.with_manifest_context(remove_sensitive: true, &type_proc)
  end
end

.subclass(command_class, scoped_namespace:, full_command_name:, command_name:, inputs_transformers:, result_transformers:, errors_transformers:, pre_commit_transformers:, serializers:, response_mutators:, request_mutators:, allowed_rule:, requires_authentication:, authenticator:, suffix: nil, capture_unknown_error: false) ⇒ Object



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 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 26

def subclass(
  command_class,
  scoped_namespace:,
  full_command_name:,
  command_name:,
  inputs_transformers:,
  result_transformers:,
  errors_transformers:,
  pre_commit_transformers:,
  serializers:,
  response_mutators:,
  request_mutators:,
  allowed_rule:,
  requires_authentication:,
  authenticator:,
  suffix: nil,
  capture_unknown_error: false
)
  Class.new(self).tap do |klass|
    klass.command_class = command_class
    klass.command_name = command_name
    klass.full_command_name = full_command_name
    klass.capture_unknown_error = capture_unknown_error
    klass.inputs_transformers = Util.array(inputs_transformers)
    klass.result_transformers = Util.array(result_transformers)
    klass.errors_transformers = Util.array(errors_transformers)
    klass.pre_commit_transformers = Util.array(pre_commit_transformers)
    klass.serializers = Util.array(serializers)
    klass.response_mutators = Util.array(response_mutators)
    klass.request_mutators = Util.array(request_mutators)
    klass.allowed_rule = allowed_rule
    klass.requires_authentication = requires_authentication
    klass.authenticator = authenticator
    klass.subclassed_in_namespace = scoped_namespace
    klass.suffix = suffix
  end
end

.transformers_to_processors(transformers, target_type, direction: :from, declaration_data: self) ⇒ Object

TODO: this is pretty messy with smells.



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 561

def transformers_to_processors(transformers, target_type, direction: :from, declaration_data: self)
  transformers.map do |transformer|
    if transformer.is_a?(Class)
      if transformer < TypeDeclarations::TypedTransformer
        transformer.new(direction => target_type).tap do |tx|
          new_type = direction == :from ? tx.to_type : tx.from_type
          target_type = new_type if new_type
        end
      else
        # TODO: perhaps pass in the command connector as the parent declaration data?
        transformer.new_with_agnostic_args(declaration_data:)
      end
    elsif transformer.is_a?(Value::Processor)
      transformer
    elsif transformer.respond_to?(:call)
      Value::Transformer.create(transform: transformer)
    else
      # :nocov:
      raise "Not sure how to apply #{inputs_transformer}"
      # :nocov:
    end
  end
end

.types_depended_onObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 286

def types_depended_on
  types = Set.new

  # TODO: memoize this
  # TODO: this should not delegate to command since transformers are in play

  types_proc = proc do
    type = inputs_type_for_manifest

    if type
      if type.registered?
        # TODO: if we ever change from attributes-only inputs type
        # then this will be handy
        # :nocov:
        types |= [type]
        # :nocov:
      end

      types |= type.types_depended_on
    end

    type = result_type

    if type
      if type.registered?
        # TODO: if we ever change from attributes-only inputs type
        # then this will be handy
        # :nocov:
        types |= [type]
        # :nocov:
      end

      types |= type.types_depended_on
    end

    possible_errors.each do |possible_error|
      error_class = possible_error.error_class
      types |= error_class.types_depended_on
    end

    types
  end

  if TypeDeclarations.manifest_context_set?(:remove_sensitive)
    types_proc.call
  else
    TypeDeclarations.with_manifest_context(remove_sensitive: true, &types_proc)
  end
end

Instance Method Details

#apply_allowed_ruleObject



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 734

def apply_allowed_rule
  rule = allowed_rule

  if rule
    command.after_load_records do |command:, **|
      is_allowed = instance_exec(&rule)

      unless is_allowed
        explanation = allowed_rule.explanation

        if explanation.is_a?(Proc)
          explanation = instance_eval(&explanation)
        end

        if explanation.nil?
          source = if allowed_rule.block.respond_to?("source") && defined?(MethodSource)
                     begin
                       # This only works when pry is loaded
                       # :nocov:
                       allowed_rule.block.source
                       # :nocov:
                     rescue MethodSource::SourceNotFoundError
                       # This path is hit if the way the source code is extracted
                       # doesn't result in valid Ruby, for example, as part of a hash such as:
                       # allowed_rule: -> () { whatever?(something) },
                     end
                   end

          source ||= allowed_rule.block.source_location.join(":")
          explanation = source || "No explanation."
        end

        error = CommandConnector::NotAllowedError.for(rule_symbol: rule.symbol, explanation:)
        self.outcome = Outcome.error(error)

        command.state_machine.error!
        command.halt!
      end
    end
  end
end

#apply_pre_commit_transformersObject



776
777
778
779
780
781
782
783
784
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 776

def apply_pre_commit_transformers
  if pre_commit_transformer
    command.before_commit_transaction do |**|
      if pre_commit_transformer.applicable?(self)
        pre_commit_transformer.process_value!(self)
      end
    end
  end
end

#authenticated_credentialObject



625
626
627
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 625

def authenticated_credential
  request.authenticated_credential
end

#authenticated_userObject



621
622
623
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 621

def authenticated_user
  request.authenticated_user
end

#construct_commandObject



730
731
732
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 730

def construct_command
  self.command = command_class.new(transformed_inputs)
end

#errorsObject



811
812
813
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 811

def errors
  outcome.errors
end

#errors_transformerObject



700
701
702
703
704
705
706
707
708
709
710
711
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 700

def errors_transformer
  return nil if errors_transformers.empty?

  transformers = self.class.transformers_to_processors(errors_transformers, nil, direction: :from,
                                                                                 declaration_data: self)

  if transformers.size == 1
    transformers.first
  else
    Value::Processor::Pipeline.new(processors: transformers)
  end
end

#flush_transactionsObject



815
816
817
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 815

def flush_transactions
  request.opened_transactions&.reverse&.each(&:flush!)
end

#inputsObject



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 646

def inputs
  return @inputs if defined?(@inputs)

  @inputs = if inputs_type
              outcome = inputs_type.process_value(untransformed_inputs)

              if outcome.success?
                outcome.result
              else
                untransformed_inputs
              end
            else
              {}
            end
end

#mutate_response(response) ⇒ Object



670
671
672
673
674
675
676
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 670

def mutate_response(response)
  mutator = self.class.response_mutator

  if mutator&.applicable?(response)
    mutator.process_value!(response)
  end
end

#pre_commit_transformerObject

TODO: memoize



714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 714

def pre_commit_transformer
  return nil if pre_commit_transformers.empty?

  transformers = self.class.transformers_to_processors(
    pre_commit_transformers,
    nil,
    declaration_data: self
  )

  if transformers.size == 1
    transformers.first
  else
    Value::Processor::Pipeline.new(processors: transformers)
  end
end

#raw_inputsObject



837
838
839
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 837

def raw_inputs
  untransformed_inputs
end

#respond_to_missing?(method_name, private = false) ⇒ Boolean

Returns:

  • (Boolean)


851
852
853
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 851

def respond_to_missing?(method_name, private = false)
  command.respond_to?(method_name, private) || super
end

#resultObject



807
808
809
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 807

def result
  outcome.result
end

#runObject



609
610
611
612
613
614
615
616
617
618
619
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 609

def run
  apply_allowed_rule
  apply_pre_commit_transformers
  set_inputs
  run_command
  # this gives us primary keys
  flush_transactions
  transform_outcome

  outcome
end

#run_commandObject



794
795
796
797
798
799
800
801
802
803
804
805
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 794

def run_command
  outcome = command.run
  self.outcome = outcome if outcome
rescue => e
  if capture_unknown_error
    self.outcome = Outcome.error(CommandConnector::UnknownError.for(e))
  else
    # :nocov:
    raise
    # :nocov:
  end
end

#serialize_result(body) ⇒ Object

TODO: kill this



829
830
831
832
833
834
835
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 829

def serialize_result(body)
  if serializer
    serializer.process_value!(body)
  else
    body
  end
end

#serializerObject

TODO: let’s get this out of here… we might want to have different serializers for different command instances of the same class. but currently serializers is set on the class. Since this class should not be concerned with serialization, we should just try to relocate this to the Request which could delegate to the registry for defaults.



688
689
690
691
692
693
694
695
696
697
698
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 688

def serializer
  return nil if serializers.empty?

  transformers = self.class.transformers_to_processors(serializers, nil, declaration_data: self)

  if transformers.size == 1
    transformers.first
  else
    Value::Processor::Pipeline.new(processors: transformers)
  end
end

#set_inputsObject



786
787
788
789
790
791
792
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 786

def set_inputs
  if self.class.inputs_type
    command.after_cast_and_validate_inputs do |**|
      inputs
    end
  end
end

#transform_errorsObject



678
679
680
681
682
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 678

def transform_errors
  if errors_transformer&.applicable?(errors)
    self.outcome = Outcome.errors(errors_transformer.process_value!(errors))
  end
end

#transform_inputsObject



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 629

def transform_inputs
  transformer = self.class.inputs_transformer

  self.transformed_inputs = if transformer&.applicable?(untransformed_inputs)
                              outcome = transformer.process_value(untransformed_inputs)

                              if outcome.success?
                                outcome.result
                              else
                                self.outcome = outcome
                                untransformed_inputs
                              end
                            else
                              untransformed_inputs
                            end
end

#transform_outcomeObject



819
820
821
822
823
824
825
826
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 819

def transform_outcome
  if outcome.success?
    # can we do this while still in the transaction of the command???
    transform_result
  else
    transform_errors
  end
end

#transform_resultObject



662
663
664
665
666
667
668
# File 'foobara-0.1.7/projects/command_connectors/src/transformed_command.rb', line 662

def transform_result
  transformer = self.class.result_transformer

  if transformer&.applicable?(result)
    self.outcome = Outcome.success(transformer.process_value!(result))
  end
end