Class: Foobara::CommandRegistry::ExposedCommand

Inherits:
Object
  • Object
show all
Includes:
IsManifestable, Scoped, TruncatedInspect
Defined in:
foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from Scoped

#foobara_manifest_reference, #scoped_namespace, #unregistered_foobara_manifest_reference

Instance Method Summary collapse

Methods included from TruncatedInspect

#inspect, truncating

Methods included from IsManifestable

#foobara_domain, #foobara_organization, #scoped_clear_caches

Methods included from Scoped

#scoped_absolute_name, #scoped_category, #scoped_clear_caches, #scoped_full_name, #scoped_full_path, #scoped_ignore_module?, #scoped_ignore_modules=, #scoped_name, #scoped_name=, #scoped_path_autoset=, #scoped_path_autoset?, #scoped_path_set?, #scoped_prefix, #scoped_reregistering!, #scoped_short_name, #scoped_short_path, #scoped_unregistered!, #scoped_unregistered?

Constructor Details

#initialize(command_class, scoped_path: nil, suffix: nil, capture_unknown_error: nil, inputs_transformers: nil, result_transformers: nil, errors_transformers: nil, pre_commit_transformers: nil, response_mutators: nil, request_mutators: nil, serializers: nil, allowed_rule: nil, requires_authentication: nil, authenticator: nil, aggregate_entities: nil, atomic_entities: nil) ⇒ ExposedCommand

Returns a new instance of ExposedCommand.



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
63
64
65
66
67
68
69
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 22

def initialize(
  command_class,
  scoped_path: nil,
  suffix: nil,
  capture_unknown_error: nil,
  inputs_transformers: nil,
  result_transformers: nil,
  errors_transformers: nil,
  pre_commit_transformers: nil,
  response_mutators: nil,
  request_mutators: nil,
  serializers: nil,
  allowed_rule: nil,
  requires_authentication: nil,
  authenticator: nil,
  aggregate_entities: nil,
  atomic_entities: nil
)
  if allowed_rule && authenticator && requires_authentication.nil?
    requires_authentication = true
  end

  if requires_authentication || allowed_rule
    errors_transformers = [
      *errors_transformers,
      Foobara::CommandConnectors::Transformers::AuthErrorsTransformer
    ].uniq
  end

  scoped_path ||= if suffix
                    *prefix, short = command_class.scoped_path
                    [*prefix, "#{short}#{suffix}"]
                  else
                    command_class.scoped_path
                  end

  if aggregate_entities
    pre_commit_transformers = [
      *pre_commit_transformers,
      CommandConnectors::Transformers::LoadAggregatesPreCommitTransformer
    ]

    pre_commit_transformers.uniq!
    pre_commit_transformers.delete(CommandConnectors::Transformers::LoadAtomsPreCommitTransformer)

    serializers = [*serializers, CommandConnectors::Serializers::AggregateSerializer]

    serializers.uniq!
    serializers.delete(Foobara::CommandConnectors::Serializers::AtomicSerializer)
  # TODO: either both should have special behavior for false or neither should
  elsif aggregate_entities == false
    pre_commit_transformers = pre_commit_transformers&.reject do |t|
      t == Foobara::CommandConnectors::Transformers::LoadAggregatesPreCommitTransformer
    end
    serializers = serializers&.reject do |s|
      s == Foobara::CommandConnectors::Serializers::AggregateSerializer
    end
  elsif atomic_entities
    pre_commit_transformers = [
      *pre_commit_transformers,
      CommandConnectors::Transformers::LoadAtomsPreCommitTransformer
    ]

    pre_commit_transformers.uniq!
    pre_commit_transformers.delete(CommandConnectors::Transformers::LoadAggregatesPreCommitTransformer)

    serializers = [*serializers, Foobara::CommandConnectors::Serializers::AtomicSerializer]

    serializers.uniq!
    serializers.delete(CommandConnectors::Serializers::AggregateSerializer)
  end

  self.command_class = command_class
  self.scoped_path = scoped_path
  self.capture_unknown_error = capture_unknown_error
  self.inputs_transformers = inputs_transformers
  self.result_transformers = result_transformers
  self.errors_transformers = errors_transformers
  self.pre_commit_transformers = pre_commit_transformers
  self.response_mutators = response_mutators
  self.request_mutators = request_mutators
  self.serializers = serializers
  self.allowed_rule = allowed_rule
  self.requires_authentication = requires_authentication
  self.authenticator = authenticator

  # A bit hacky... we should check if we need to shim in a LoadDelegatedAttributesEntitiesPreCommitTransformer
  unless aggregate_entities
    # It's possible delegates have been added or removed via the result transformers...
    # We should figure out a way to check the transformed result type instead.
    if _has_delegated_attributes?(command_class.result_type)
      self.pre_commit_transformers = [
        *self.pre_commit_transformers,
        CommandConnectors::Transformers::LoadDelegatedAttributesEntitiesPreCommitTransformer
      ]

      pre_commit_transformers.uniq!
    end
  end

  Namespace.on_change(self, :clear_caches)
end

Instance Attribute Details

#allowed_ruleObject

Returns the value of attribute allowed_rule.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def allowed_rule
  @allowed_rule
end

#authenticatorObject

Returns the value of attribute authenticator.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def authenticator
  @authenticator
end

#capture_unknown_errorObject

Returns the value of attribute capture_unknown_error.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def capture_unknown_error
  @capture_unknown_error
end

#command_classObject

Returns the value of attribute command_class.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def command_class
  @command_class
end

#errors_transformersObject

Returns the value of attribute errors_transformers.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def errors_transformers
  @errors_transformers
end

#inputs_transformersObject

Returns the value of attribute inputs_transformers.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def inputs_transformers
  @inputs_transformers
end

#pre_commit_transformersObject

Returns the value of attribute pre_commit_transformers.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def pre_commit_transformers
  @pre_commit_transformers
end

#request_mutatorsObject

Returns the value of attribute request_mutators.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def request_mutators
  @request_mutators
end

#requires_authenticationObject

Returns the value of attribute requires_authentication.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def requires_authentication
  @requires_authentication
end

#response_mutatorsObject

Returns the value of attribute response_mutators.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def response_mutators
  @response_mutators
end

#result_transformersObject

Returns the value of attribute result_transformers.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def result_transformers
  @result_transformers
end

#scoped_pathObject

Returns the value of attribute scoped_path.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def scoped_path
  @scoped_path
end

#serializersObject

Returns the value of attribute serializers.



8
9
10
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 8

def serializers
  @serializers
end

Instance Method Details

#_has_delegated_attributes?(type) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 129

def _has_delegated_attributes?(type)
  type&.extends?(BuiltinTypes[:model]) && type.target_class&.has_delegated_attributes?
end

#clear_cachesObject



125
126
127
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 125

def clear_caches
  @transformed_command_class = nil
end

#command_nameObject



137
138
139
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 137

def command_name
  @command_name ||= Util.non_full_name(full_command_name)
end

#foobara_manifestObject



145
146
147
148
149
150
151
152
153
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 145

def foobara_manifest
  transformed_command_class.foobara_manifest.merge(super).merge(
    Util.remove_blank(
      scoped_category: :command,
      domain: command_class.domain.foobara_manifest_reference,
      organization: command_class.organization.foobara_manifest_reference
    )
  )
end

#full_command_nameObject



133
134
135
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 133

def full_command_name
  scoped_full_name
end

#full_command_symbolObject



141
142
143
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 141

def full_command_symbol
  @full_command_symbol ||= Util.underscore_sym(full_command_name)
end

#result_has_sensitive_types?Boolean

TODO: what to do if the whole return type is sensitive? return nil?

Returns:

  • (Boolean)


195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 195

def result_has_sensitive_types?
  result_type = command_class.result_type

  if result_type.nil?
    false
  elsif result_type.sensitive?
    # :nocov:
    # TODO: we should convert it to nil I suppose
    raise "Not sure yet how to handle a sensitive result type hmmmm..."
    # :nocov:
  else
    command_class.result_type.has_sensitive_types?
  end
end

#transformed_command_classObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'foobara-0.1.16/projects/command_connectors/src/command_registry/exposed_command.rb', line 155

def transformed_command_class
  @transformed_command_class ||= if Util.all_blank_or_false?(
    [
      capture_unknown_error,
      inputs_transformers,
      result_transformers,
      errors_transformers,
      pre_commit_transformers,
      response_mutators,
      request_mutators,
      serializers,
      allowed_rule,
      requires_authentication,
      authenticator,
      result_has_sensitive_types?
    ]
  ) && scoped_path == command_class.scoped_path
                                   command_class
                                 else
                                   Foobara::TransformedCommand.subclass(
                                     command_class,
                                     scoped_namespace:,
                                     full_command_name:,
                                     command_name:,
                                     capture_unknown_error:,
                                     inputs_transformers:,
                                     result_transformers:,
                                     errors_transformers:,
                                     pre_commit_transformers:,
                                     response_mutators:,
                                     request_mutators:,
                                     serializers:,
                                     allowed_rule:,
                                     requires_authentication:,
                                     authenticator:
                                   )
                                 end
end