Module: Foobara::Domain::DomainModuleExtension::ClassMethods

Defined in:
foobara-0.0.110/projects/domain/src/domain_module_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#foobara_domain_nameObject



188
189
190
191
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 188

def foobara_domain_name
  # TODO: does this work properly with prefixes?
  @foobara_domain_name || scoped_name
end

#foobara_full_domain_nameObject



193
194
195
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 193

def foobara_full_domain_name
  @foobara_full_domain_name || scoped_full_name
end

Instance Method Details

#_set_type_constant(type) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 468

def _set_type_constant(type)
  domain = if scoped_full_path.empty?
             GlobalDomain
           else
             self
           end

  path = type.scoped_path
  if path.first == "Types"
    path = path[1..]
  end

  types_mod = if domain.const_defined?(:Types)
                domain.const_get(:Types)
              else
                domain.const_set(:Types, Module.new)
              end

  if type.scoped_prefix
    const_name = [types_mod.name, *path[0..-2]].join("::")
    types_mod = Util.make_module_p(const_name, tag: true)
  end

  # TODO: dry this up
  if type.scoped_short_name =~ /\A[a-z]/
    unless types_mod.respond_to?(type.scoped_short_name)
      types_mod.singleton_class.define_method type.scoped_short_name do
        type
      end

      unless types_mod.instance_variable_defined?(:@foobara_lowercase_constants)
        types_mod.instance_variable_set(:@foobara_lowercase_constants, [])
      end

      types_mod.instance_variable_get(:@foobara_lowercase_constants) << type.scoped_short_name
    end
  elsif types_mod.const_defined?(type.scoped_short_name, false)
    existing_value = types_mod.const_get(type.scoped_short_name)
    existing_value_type = if existing_value.is_a?(::Class) && existing_value < Foobara::Model
                            # TODO: test this code path
                            # :nocov:
                            existing_value.model_type
                            # :nocov:
                          else
                            existing_value
                          end

    if existing_value_type != type
      if existing_value.is_a?(::Module) && !existing_value.is_a?(::Class) &&
         existing_value.instance_variable_get(:@foobara_created_via_make_class) &&
         # not allowing lower-case "constants" to be namespaces
         type.extends?("::model")

        types_mod.send(:remove_const, type.scoped_short_name)
        types_mod.const_set(type.scoped_short_name, type.target_class)

        DomainModuleExtension._copy_constants(existing_value, type.target_class)
      else
        # :nocov:
        raise CannotSetTypeConstantError,
              "Already defined constant #{types_mod.name}::#{type.scoped_short_name}"
        # :nocov:
      end
    end
  else
    symbol = type.scoped_short_name

    if type.extends?("::model")
      type = type.target_class
    end

    types_mod.const_set(symbol, type)
  end
end

#foobara_can_call_subcommands_from?(other_domain) ⇒ Boolean

Returns:

  • (Boolean)


396
397
398
399
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 396

def foobara_can_call_subcommands_from?(other_domain)
  other_domain = Domain.to_domain(other_domain)
  other_domain == self || self == GlobalDomain || foobara_depends_on?(other_domain)
end

#foobara_command_classesObject



256
257
258
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 256

def foobara_command_classes
  foobara_all_command(mode: Namespace::LookupMode::DIRECT)
end

#foobara_depends_on(*domains) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 406

def foobara_depends_on(*domains)
  if domains.empty?
    return @foobara_depends_on ||= Set.new
  end

  if domains.length == 1
    domains = Util.array(domains.first)
  end

  domains.each do |domain|
    # It very likely could be a module extended with domain methods...
    domain = Domain.to_domain(domain)
    domain_name = domain.foobara_full_domain_name

    if foobara_depends_on.include?(domain_name)
      # :nocov:
      raise AlreadyRegisteredDomainDependency, "Already registered #{domain_name} as a dependency of #{self}"
      # :nocov:
    end

    foobara_depends_on_namespaces << domain
    foobara_type_builder.accesses << domain.foobara_type_builder

    foobara_depends_on << domain_name
  end
end

#foobara_depends_on?(other_domain) ⇒ Boolean

Returns:

  • (Boolean)


401
402
403
404
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 401

def foobara_depends_on?(other_domain)
  other_domain = Domain.to_domain(other_domain)
  other_domain == GlobalDomain || foobara_depends_on.include?(other_domain.foobara_full_domain_name)
end

#foobara_domain?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 222

def foobara_domain?
  true
end

#foobara_domain_map(*args, to: nil, strict: false, criteria: nil, should_raise: false, **opts) ⇒ Object



142
143
144
145
146
147
148
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
174
175
176
177
178
179
180
181
182
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 142

def foobara_domain_map(*args, to: nil, strict: false, criteria: nil, should_raise: false, **opts)
  case args.size
  when 1
    value = args.first
  when 0
    if opts.empty?
      # :nocov:
      raise ArgumentError, "Expected at least one argument"
      # :nocov:
    else
      value = opts
      opts = {}
    end
  else
    # :nocov:
    raise ArgumentError, "Expected 1 argument but got #{args.size}"
    # :nocov:
  end

  invalid_keys = opts.keys - [:from]

  if invalid_keys.any?
    # :nocov:
    raise ArgumentError, "Invalid options: #{invalid_keys.join(", ")}"
    # :nocov:
  end

  from = if opts.key?(:from)
           opts[:from]
         else
           value
         end

  mapper = lookup_matching_domain_mapper(from:, to:, criteria:, strict:)

  if mapper
    mapper.map!(value)
  elsif should_raise
    raise Foobara::DomainMapperLookups::NoDomainMapperFoundError.new(from, to, value:)
  end
end

#foobara_domain_map!Object



184
185
186
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 184

def foobara_domain_map!(*, **, &)
  foobara_domain_map(*, should_raise: true, **, &)
end

#foobara_full_domain_symbolObject



197
198
199
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 197

def foobara_full_domain_symbol
  Util.underscore_sym(foobara_full_domain_name)
end

#foobara_full_organization_nameObject



205
206
207
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 205

def foobara_full_organization_name
  foobara_organization&.foobara_full_organization_name
end

#foobara_manifestObject

TODO: can we kill this skip concept?



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
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 434

def foobara_manifest
  to_include = TypeDeclarations.foobara_manifest_context_to_include

  depends_on = foobara_depends_on.map do |name|
    domain = Domain.to_domain(name)
    if to_include
      to_include << domain
    end
    domain.foobara_manifest_reference
  end.sort

  commands = foobara_all_command(mode: Namespace::LookupMode::DIRECT).map do |command_class|
    if to_include
      to_include << command_class
    end
    command_class.foobara_manifest_reference
  end.sort

  types = foobara_all_type(mode: Namespace::LookupMode::DIRECT).map do |type|
    if to_include
      to_include << type
    end
    type.foobara_manifest_reference
  end.sort

  manifest = super.merge(commands:, types:)

  unless depends_on.empty?
    manifest[:depends_on] = depends_on
  end

  manifest
end

#foobara_organizationObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 209

def foobara_organization
  parent = foobara_parent_namespace

  while parent
    return parent if parent&.foobara_organization?

    # TODO: we really should test this path
    # :nocov:
    parent = parent.foobara_parent_namespace
    # :nocov:
  end || GlobalOrganization
end

#foobara_organization_nameObject



201
202
203
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 201

def foobara_organization_name
  foobara_organization&.foobara_organization_name
end

#foobara_register_and_deanonymize_entities(entity_names_to_attributes) ⇒ Object



376
377
378
379
380
381
382
383
384
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 376

def foobara_register_and_deanonymize_entities(entity_names_to_attributes)
  entities = []

  entity_names_to_attributes.each_pair do |entity_name, attributes_declaration|
    entities << foobara_register_and_deanonymize_entity(entity_name, attributes_declaration)
  end

  entities
end

#foobara_register_and_deanonymize_entity(name) ⇒ Object



307
308
309
310
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 307

def foobara_register_and_deanonymize_entity(name, *, &)
  entity_class = foobara_register_entity(name, *, &)
  Foobara::Model.deanonymize_class(entity_class)
end

#foobara_register_entities(entity_names_to_attributes) ⇒ Object



386
387
388
389
390
391
392
393
394
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 386

def foobara_register_entities(entity_names_to_attributes)
  entities = []

  entity_names_to_attributes.each_pair do |entity_name, attributes_type_declaration|
    entities << foobara_register_entity(entity_name, attributes_type_declaration)
  end

  entities
end

#foobara_register_entity(name, *args, &block) ⇒ Object

TODO: kill this off



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 313

def foobara_register_entity(name, *args, &block)
  # TODO: introduce a Namespace#scope method to simplify this a bit
  Foobara::Namespace.use self do
    if block
      args = [TypeDeclarations::Dsl::Attributes.to_declaration(&block), *args]
    end

    attributes_type_declaration, *args = args

    model_base_class, description = case args.size
                                    when 0
                                      []
                                    when 1, 2
                                      arg, other = args

                                      if args.first.is_a?(::String)
                                        [other, arg]
                                      else
                                        args
                                      end
                                    else
                                      # :nocov:
                                      raise ArgumentError, "Too many arguments"
                                      # :nocov:
                                    end

    if model_base_class
      attributes_type_declaration = TypeDeclarations::Attributes.merge(
        model_base_class.attributes_type.declaration_data,
        attributes_type_declaration
      )
    end

    handler = foobara_type_builder.handler_for_class(
      Foobara::TypeDeclarations::Handlers::ExtendAttributesTypeDeclaration
    )

    attributes_type = handler.type_for_declaration(attributes_type_declaration)

    # TODO: reuse the model_base_class primary key if it has one...
    primary_key = attributes_type.element_types.keys.first

    model_module = unless scoped_full_path.empty?
                     scoped_full_name
                   end

    entity_type = foobara_type_builder.type_for_declaration(
      Util.remove_blank(
        type: :entity,
        name:,
        model_base_class:,
        model_module:,
        attributes_declaration: attributes_type_declaration,
        primary_key:,
        description:,
        _desugarized: { type_absolutified: true }
      )
    )

    entity_type.target_class
  end
end

#foobara_register_model(model_class) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 285

def foobara_register_model(model_class)
  type = model_class.model_type

  full_name = type.scoped_full_name

  if model_class.full_model_name.size > full_name.size
    # TODO: why does this happen exactly??
    full_name = model_class.full_model_name
  end

  if type.scoped_path_set? && foobara_registered?(full_name, mode: Namespace::LookupMode::DIRECT)
    # :nocov:
    raise AlreadyRegisteredError, "Already registered: #{type.inspect}"
    # :nocov:
  end

  foobara_register(type)
  type.foobara_parent_namespace = self

  type.target_class
end

#foobara_register_type(type_symbol, *type_declaration_bits, &block) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 260

def foobara_register_type(type_symbol, *type_declaration_bits, &block)
  type = if block.nil? && type_declaration_bits.size == 1 && type_declaration_bits.first.is_a?(Types::Type)
           type_declaration_bits.first
         else
           foobara_type_from_declaration(*type_declaration_bits, &block)
         end

  if type_symbol.is_a?(::Array)
    type.scoped_path = type_symbol
    type.type_symbol = type_symbol.join("::")
  else
    type_symbol = type_symbol.to_s if type_symbol.is_a?(::Symbol)

    type.scoped_path = type_symbol.split("::")
    type.type_symbol = type_symbol.to_sym
  end

  type.foobara_parent_namespace ||= self
  foobara_register(type)

  _set_type_constant(type)

  type
end

#foobara_type_builderObject

TODO: kill this off



227
228
229
230
231
232
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 227

def foobara_type_builder
  @foobara_type_builder ||= begin
    accesses = self == GlobalDomain ? [] : GlobalDomain.foobara_type_builder
    TypeDeclarations::TypeBuilder.new(foobara_full_domain_name, accesses:)
  end
end

#foobara_type_from_declaration(*args, **opts, &block) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 234

def foobara_type_from_declaration(*args, **opts, &block)
  if opts.empty? && block.nil? && args.size == 1 && args.first.is_a?(Types::Type)
    return args.first
  end

  Foobara::Namespace.use self do
    foobara_type_builder.type_for_declaration(*args, **opts, &block)
  end
end

#foobara_type_from_strict_declarationObject



250
251
252
253
254
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 250

def foobara_type_from_strict_declaration(...)
  Foobara::Namespace.use self do
    foobara_type_builder.type_for_strict_declaration(...)
  end
end

#foobara_type_from_strict_stringified_declarationObject



244
245
246
247
248
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 244

def foobara_type_from_strict_stringified_declaration(...)
  Foobara::Namespace.use self do
    foobara_type_builder.type_for_strict_stringified_declaration(...)
  end
end

#foobara_unregister(scoped) ⇒ Object



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'foobara-0.0.110/projects/domain/src/domain_module_extension.rb', line 65

def foobara_unregister(scoped)
  foobara_type_builder.clear_cache

  if scoped.is_a?(Foobara::Types::Type)
    parent_mod = nil

    if const_defined?(:Types, false)
      parent_path = ["Foobara::GlobalDomain"]
      unless scoped.type_symbol.to_s.start_with?("Types::")
        parent_path << "Types"
      end
      parent_path += scoped.type_symbol.to_s.split("::")[..-2]

      parent_name = parent_path.join("::")
      child_name = [*parent_path, scoped.type_symbol.to_s.split("::").last].join("::")
      removed = false

      if Object.const_defined?(parent_name)
        parent_mod = Object.const_get(parent_name)

        if scoped.scoped_short_name =~ /^[a-z]/
          lower_case_constants = parent_mod.instance_variable_get(:@foobara_lowercase_constants)

          if lower_case_constants&.include?(scoped.scoped_short_name)
            parent_mod.singleton_class.undef_method scoped.scoped_short_name
            lower_case_constants.delete(scoped.scoped_short_name)
          end

          removed = true
        elsif parent_mod.const_defined?(scoped.scoped_short_name, false)
          parent_mod.send(:remove_const, scoped.scoped_short_name)
          removed = true
        end
      end

      if removed
        child_name = parent_name

        while child_name
          child = Object.const_get(child_name)

          break if child.foobara_domain?

          break if child.foobara_organization?

          # TODO: unclear why we need this check, hmmm, figure it out and document it (or delete if not needed)
          break if child.constants(false).any? do |constant|
            # TODO: a clue: this stopped being entered by the test suite after deleting GlobalDomain::Types
            # in .reset_alls hmmmmmm...
            # :nocov:
            value = child.const_get(constant)

            # TODO: can we make this not coupled to model project??
            value.is_a?(Types::Type) || (value.is_a?(::Class) && value < Foobara::Model)
            # :nocov:
          end

          lower_case_constants = child.instance_variable_get(:@foobara_lowercase_constants)
          break if lower_case_constants && !lower_case_constants.empty?

          parent_name = Util.parent_module_name_for(child_name)
          break unless Object.const_defined?(parent_name)

          parent = Object.const_get(parent_name)

          child_sym = Util.non_full_name(child).to_sym
          parent.send(:remove_const, child_sym)

          child_name = parent_name
        end
      end
    end
  end

  super
end