Class: Foobara::CommandRegistry
- Inherits:
-
Object
- Object
- Foobara::CommandRegistry
- Includes:
- TruncatedInspect
- Defined in:
- foobara-0.2.6/projects/command_connectors/src/command_registry/allowed_rule.rb,
foobara-0.2.6/projects/command_connectors/src/command_registry.rb,
foobara-0.2.6/projects/command_connectors/src/command_registry/exposed_domain.rb,
foobara-0.2.6/projects/command_connectors/src/command_registry/exposed_command.rb,
foobara-0.2.6/projects/command_connectors/src/command_registry/exposed_organization.rb
Overview
TODO: move to foobara monorepo if this is generic…
Defined Under Namespace
Modules: ExposedDomain, ExposedOrganization Classes: AllowedRule, ExposedCommand
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary collapse
-
#authenticator ⇒ Object
Returns the value of attribute authenticator.
-
#default_allowed_rule ⇒ Object
Returns the value of attribute default_allowed_rule.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_default_errors_transformer(transformer) ⇒ Object
- #add_default_inputs_transformer(transformer) ⇒ Object
- #add_default_pre_commit_transformer(transformer) ⇒ Object
- #add_default_result_transformer(transformer) ⇒ Object
- #add_default_serializer(serializer) ⇒ Object
- #all_transformed_command_classes ⇒ Object
- #allowed_rule ⇒ Object
- #allowed_rule_registry ⇒ Object
- #allowed_rules(hash) ⇒ Object
- #apply_defaults(inputs_transformers: nil, result_transformers: nil, errors_transformers: nil, pre_commit_transformers: nil, serializers: nil, allowed_rule: default_allowed_rule, authenticator: nil, **opts) ⇒ Object
- #build_and_register_exposed_domain(domain_full_name) ⇒ Object
- #build_and_register_exposed_organization(full_organization_name) ⇒ Object
- #create_exposed_command(command_class) ⇒ Object
-
#create_exposed_command_without_domain(command_class) ⇒ Object
TODO: eliminate this method.
- #default_errors_transformers ⇒ Object
- #default_inputs_transformers ⇒ Object
- #default_pre_commit_transformers ⇒ Object
- #default_result_transformers ⇒ Object
- #default_serializers ⇒ Object
- #each_transformed_command_class ⇒ Object
- #global_domain ⇒ Object
- #global_organization ⇒ Object
-
#initialize(authenticator: nil) ⇒ CommandRegistry
constructor
Should we support different authenticators for different commands? Might be a smell of two domains co-habitating in one? Or maybe one is just passing another through and we should support that?.
- #register(command_class) ⇒ Object
- #size ⇒ Object
- #to_allowed_rule(*args) ⇒ Object
- #transformed_command_from_name(name) ⇒ Object
Methods included from TruncatedInspect
Constructor Details
#initialize(authenticator: nil) ⇒ CommandRegistry
Should we support different authenticators for different commands? Might be a smell of two domains co-habitating in one? Or maybe one is just passing another through and we should support that?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'projects/command_connectors/src/command_registry.rb', line 12 def initialize(authenticator: nil) self.scoped_path = [] self.authenticator = authenticator customized = [:command] Namespace.global..keys.reverse.each do |symbol| next if customized.include?(symbol) proc = Namespace.global.[symbol] (symbol, &proc) end (:command, ExposedCommand) << Namespace.global end |
Instance Attribute Details
#authenticator ⇒ Object
Returns the value of attribute authenticator.
7 8 9 |
# File 'projects/command_connectors/src/command_registry.rb', line 7 def authenticator @authenticator end |
#default_allowed_rule ⇒ Object
Returns the value of attribute default_allowed_rule.
7 8 9 |
# File 'projects/command_connectors/src/command_registry.rb', line 7 def default_allowed_rule @default_allowed_rule end |
Instance Method Details
#[](name) ⇒ Object
141 142 143 144 145 146 147 |
# File 'projects/command_connectors/src/command_registry.rb', line 141 def [](name) if name.is_a?(Class) name.full_command_name end (name) end |
#add_default_errors_transformer(transformer) ⇒ Object
194 195 196 |
# File 'projects/command_connectors/src/command_registry.rb', line 194 def add_default_errors_transformer(transformer) default_errors_transformers << transformer end |
#add_default_inputs_transformer(transformer) ⇒ Object
178 179 180 |
# File 'projects/command_connectors/src/command_registry.rb', line 178 def add_default_inputs_transformer(transformer) default_inputs_transformers << transformer end |
#add_default_pre_commit_transformer(transformer) ⇒ Object
202 203 204 |
# File 'projects/command_connectors/src/command_registry.rb', line 202 def add_default_pre_commit_transformer(transformer) default_pre_commit_transformers << transformer end |
#add_default_result_transformer(transformer) ⇒ Object
186 187 188 |
# File 'projects/command_connectors/src/command_registry.rb', line 186 def add_default_result_transformer(transformer) default_result_transformers << transformer end |
#add_default_serializer(serializer) ⇒ Object
210 211 212 |
# File 'projects/command_connectors/src/command_registry.rb', line 210 def add_default_serializer(serializer) default_serializers << serializer end |
#all_transformed_command_classes ⇒ Object
296 297 298 |
# File 'projects/command_connectors/src/command_registry.rb', line 296 def all_transformed_command_classes .map(&:transformed_command_class) end |
#allowed_rule ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'projects/command_connectors/src/command_registry.rb', line 153 def allowed_rule(*) allowed_rule = to_allowed_rule(*) unless allowed_rule.symbol # :nocov: raise "Cannot register a rule without a symbol" # :nocov: end allowed_rule_registry[allowed_rule.symbol] = allowed_rule end |
#allowed_rule_registry ⇒ Object
149 150 151 |
# File 'projects/command_connectors/src/command_registry.rb', line 149 def allowed_rule_registry @allowed_rule_registry ||= {} end |
#allowed_rules(hash) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'projects/command_connectors/src/command_registry.rb', line 165 def allowed_rules(hash) hash.map do |symbol, ruleish| allowed_rule = to_allowed_rule(ruleish) allowed_rule.symbol = symbol allowed_rule(allowed_rule) end end |
#apply_defaults(inputs_transformers: nil, result_transformers: nil, errors_transformers: nil, pre_commit_transformers: nil, serializers: nil, allowed_rule: default_allowed_rule, authenticator: nil, **opts) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'projects/command_connectors/src/command_registry.rb', line 53 def apply_defaults( inputs_transformers: nil, result_transformers: nil, errors_transformers: nil, pre_commit_transformers: nil, serializers: nil, allowed_rule: default_allowed_rule, authenticator: nil, **opts ) opts.merge( inputs_transformers: [*inputs_transformers, *default_inputs_transformers], result_transformers: [*result_transformers, *default_result_transformers], errors_transformers: [*errors_transformers, *default_errors_transformers], pre_commit_transformers: [*pre_commit_transformers, *default_pre_commit_transformers], serializers: [*serializers, *default_serializers], allowed_rule: allowed_rule && to_allowed_rule(allowed_rule), authenticator: authenticator || self.authenticator ) end |
#build_and_register_exposed_domain(domain_full_name) ⇒ Object
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 |
# File 'projects/command_connectors/src/command_registry.rb', line 74 def build_and_register_exposed_domain(domain_full_name) domain_module = if domain_full_name.to_s == "" GlobalDomain else Namespace.global.(domain_full_name) end full_organization_name = domain_module. exposed_organization = ( full_organization_name, mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE ) || build_and_register_exposed_organization(full_organization_name) exposed_domain = Module.new exposed_domain. exposed_domain. exposed_domain.extend(ExposedDomain) exposed_domain.unexposed_domain = domain_module exposed_domain. domain_module exposed_organization.(exposed_domain) domain_module..each do |domain_name| # TODO: test this code path!! # :nocov: unless (domain_name) build_and_register_exposed_domain(domain_name) end # :nocov: end exposed_domain end |
#build_and_register_exposed_organization(full_organization_name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'projects/command_connectors/src/command_registry.rb', line 123 def build_and_register_exposed_organization(full_organization_name) org = if full_organization_name.to_s == "" GlobalOrganization else Namespace.global.(full_organization_name) end exposed_organization = Module.new exposed_organization. exposed_organization. exposed_organization.extend(ExposedOrganization) exposed_organization.unexposed_organization = org (exposed_organization) exposed_organization end |
#create_exposed_command(command_class) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'projects/command_connectors/src/command_registry.rb', line 35 def create_exposed_command(command_class, **) full_domain_name = command_class.domain.scoped_full_name exposed_domain = (full_domain_name, mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE) || build_and_register_exposed_domain(full_domain_name) exposed_command = create_exposed_command_without_domain(command_class, **) exposed_domain.(exposed_command) exposed_command end |
#create_exposed_command_without_domain(command_class) ⇒ Object
TODO: eliminate this method
49 50 51 |
# File 'projects/command_connectors/src/command_registry.rb', line 49 def create_exposed_command_without_domain(command_class, **) ExposedCommand.new(command_class, **apply_defaults(**)) end |
#default_errors_transformers ⇒ Object
190 191 192 |
# File 'projects/command_connectors/src/command_registry.rb', line 190 def default_errors_transformers @default_errors_transformers ||= [] end |
#default_inputs_transformers ⇒ Object
174 175 176 |
# File 'projects/command_connectors/src/command_registry.rb', line 174 def default_inputs_transformers @default_inputs_transformers ||= [] end |
#default_pre_commit_transformers ⇒ Object
198 199 200 |
# File 'projects/command_connectors/src/command_registry.rb', line 198 def default_pre_commit_transformers @default_pre_commit_transformers ||= [] end |
#default_result_transformers ⇒ Object
182 183 184 |
# File 'projects/command_connectors/src/command_registry.rb', line 182 def default_result_transformers @default_result_transformers ||= [] end |
#default_serializers ⇒ Object
206 207 208 |
# File 'projects/command_connectors/src/command_registry.rb', line 206 def default_serializers @default_serializers ||= [] end |
#each_transformed_command_class ⇒ Object
300 301 302 |
# File 'projects/command_connectors/src/command_registry.rb', line 300 def each_transformed_command_class(&) .map(&:transformed_command_class).each(&) end |
#global_domain ⇒ Object
110 111 112 113 |
# File 'projects/command_connectors/src/command_registry.rb', line 110 def global_domain ("", mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE) || build_and_register_exposed_domain("") end |
#global_organization ⇒ Object
115 116 117 118 119 120 121 |
# File 'projects/command_connectors/src/command_registry.rb', line 115 def global_organization # TODO: test this # :nocov: ("", mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE) || build_and_register_exposed_organization("") # :nocov: end |
#register(command_class) ⇒ Object
31 32 33 |
# File 'projects/command_connectors/src/command_registry.rb', line 31 def register(command_class, **) create_exposed_command(command_class, **) end |
#size ⇒ Object
304 305 306 |
# File 'projects/command_connectors/src/command_registry.rb', line 304 def size .size end |
#to_allowed_rule(*args) ⇒ Object
214 215 216 217 218 219 220 221 222 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'projects/command_connectors/src/command_registry.rb', line 214 def to_allowed_rule(*args) symbol, object = case args.size when 1 [nil, args.first] when 2 args else # :nocov: raise ArgumentError, "Expected 1 or 2 arguments, got #{args.size}" # :nocov: end case object when AllowedRule, nil object when ::String if symbol # :nocov: raise ArgumentError, "Was not expecting a symbol and a string" # :nocov: end to_allowed_rule(object.to_sym) when ::Symbol allowed_rule = allowed_rule_registry[object] unless allowed_rule # :nocov: raise "No allowed rule found for #{object}" # :nocov: end allowed_rule when ::Hash rule_attributes = AllowedRule.allowed_rule_attributes_type.process_value!(object) allowed_rule = to_allowed_rule(rule_attributes[:logic]) if rule_attributes.key?(:symbol) allowed_rule.symbol = rule_attributes[:symbol] end if rule_attributes.key?(:explanation) allowed_rule.explanation = rule_attributes[:explanation] end allowed_rule.explanation ||= allowed_rule.symbol allowed_rule when ::Array rules = object.map { |ruleish| to_allowed_rule(ruleish) } procs = rules.map(&:block) block = proc do procs.any?(&:call) end allowed_rule = AllowedRule.new(&block) if rules.all?(&:explanation) allowed_rule.explanation = Util.to_or_sentence(rules.map(&:explanation)) end allowed_rule else if object.respond_to?(:call) AllowedRule.new(symbol:, &object) else # :nocov: raise "Not sure how to convert #{object} into an AllowedRule object" # :nocov: end end.tap do |rule| rule.symbol ||= symbol end end |
#transformed_command_from_name(name) ⇒ Object
292 293 294 |
# File 'projects/command_connectors/src/command_registry.rb', line 292 def transformed_command_from_name(name) (name, mode: Namespace::LookupMode::RELAXED)&.transformed_command_class end |