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

Defined in:
foobara-0.2.2/projects/domain/src/domain_module_extension.rb,
foobara-0.2.2/projects/entity/src/extensions/domain/domain_module_extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#foobara_default_entity_baseObject (readonly)

Returns the value of attribute foobara_default_entity_base.



5
6
7
# File 'foobara-0.2.2/projects/entity/src/extensions/domain/domain_module_extension.rb', line 5

def foobara_default_entity_base
  @foobara_default_entity_base
end

#foobara_domain_nameObject



180
181
182
183
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 180

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

#foobara_full_domain_nameObject



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

def foobara_full_domain_name
  @foobara_full_domain_name || scoped_full_name
end

Instance Method Details

#_set_type_constant(type) ⇒ Object



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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 502

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)


430
431
432
433
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 430

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



248
249
250
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 248

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

#foobara_depends_on(*domains) ⇒ Object



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.2.2/projects/domain/src/domain_module_extension.rb', line 440

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

    lru_cache.reset!
    foobara_depends_on << domain_name
  end
end

#foobara_depends_on?(other_domain) ⇒ Boolean

Returns:

  • (Boolean)


435
436
437
438
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 435

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)


214
215
216
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 214

def foobara_domain?
  true
end

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



134
135
136
137
138
139
140
141
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
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 134

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



176
177
178
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 176

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

#foobara_full_domain_symbolObject



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

def foobara_full_domain_symbol
  Util.underscore_sym(foobara_full_domain_name)
end

#foobara_full_organization_nameObject



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

def foobara_full_organization_name
  foobara_organization&.foobara_full_organization_name
end

#foobara_manifestObject



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

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



201
202
203
204
205
206
207
208
209
210
211
212
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 201

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



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

def foobara_organization_name
  foobara_organization&.foobara_organization_name
end

#foobara_register_and_deanonymize_entities(entity_names_to_attributes) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 410

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



334
335
336
337
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 334

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



420
421
422
423
424
425
426
427
428
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 420

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



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

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).declaration_data,
        *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

    declaration = TypeDeclaration.new(
      Util.remove_blank(
        type: :entity,
        name:,
        model_base_class:,
        model_module:,
        attributes_declaration: attributes_type_declaration,
        primary_key:,
        description:
      )
    )

    declaration.is_absolutified = true
    declaration.is_duped = true

    entity_type = foobara_type_builder.type_for_declaration(declaration)

    entity_type.target_class
  end
end

#foobara_register_model(model_class) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 313

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.target_class
end

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



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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 252

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

  new_scoped_path, new_type_symbol = if type_symbol.is_a?(::Array)
                                       [type_symbol, type_symbol.join("::").to_sym]
                                     else
                                       type_symbol = type_symbol.to_s if type_symbol.is_a?(::Symbol)

                                       [type_symbol.split("::"), type_symbol.to_sym]
                                     end

  if type.scoped_path_set? && type.registered? && foobara_registered?(type, mode: Namespace::LookupMode::DIRECT)
    old_symbol = type.type_symbol
    old_type = foobara_lookup_type(old_symbol, mode: Namespace::LookupMode::DIRECT)

    if old_symbol != new_type_symbol
      foobara_unregister(type)

      type.scoped_path = new_scoped_path
      type.type_symbol = new_type_symbol

      foobara_register(type)
      # :nocov:
    elsif old_type != type
      # TODO: delete this check if it's not really helping

      raise "Didn't expect to find an old type"
      # :nocov:
    end
  else
    type.scoped_path = new_scoped_path
    type.type_symbol = new_type_symbol

    old_type = foobara_lookup_type(new_type_symbol, mode: Namespace::LookupMode::DIRECT)

    if old_type && old_type != type
      # TODO: delete this check if it's not really helping
      # :nocov:
      raise "Didn't expect to find an old type"
      # :nocov:
    end

    if foobara_registered?(type, mode: Namespace::LookupMode::DIRECT)
      # TODO: delete this check if it's not really helping
      # :nocov:
      raise "Already registered: #{type.inspect}"
      # :nocov:
    end

    foobara_register(type)
  end

  _set_type_constant(type)

  type
end

#foobara_set_entity_base(name: nil, table_prefix: nil) ⇒ Object



7
8
9
10
11
# File 'foobara-0.2.2/projects/entity/src/extensions/domain/domain_module_extension.rb', line 7

def foobara_set_entity_base(*, name: nil, table_prefix: nil)
  name ||= Util.underscore(scoped_full_name).gsub("::", "_")
  base = Persistence.register_base(*, name:, table_prefix:)
  @foobara_default_entity_base = base
end

#foobara_type_builderObject

TODO: kill this off



219
220
221
222
223
224
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 219

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



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

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



242
243
244
245
246
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 242

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



236
237
238
239
240
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 236

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



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
124
125
126
127
128
129
130
131
132
# File 'foobara-0.2.2/projects/domain/src/domain_module_extension.rb', line 57

def foobara_unregister(scoped)
  scoped = to_scoped(scoped)

  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