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

Defined in:
foobara-0.5.8/projects/command/src/extensions/domain/domain_module_extension.rb,
foobara-0.5.8/projects/typesystem/projects/domain/src/domain_module_extension.rb,
foobara-0.5.8/projects/entities/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 'projects/entities/projects/entity/src/extensions/domain/domain_module_extension.rb', line 5

def foobara_default_entity_base
  @foobara_default_entity_base
end

#foobara_domain_nameObject



66
67
68
69
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 66

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

#foobara_full_domain_nameObject



71
72
73
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 71

def foobara_full_domain_name
  @foobara_full_domain_name || scoped_full_name
end

Instance Method Details

#foobara_can_call_subcommands_from?(other_domain) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'projects/command/src/extensions/domain/domain_module_extension.rb', line 9

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



5
6
7
# File 'projects/command/src/extensions/domain/domain_module_extension.rb', line 5

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

#foobara_depends_on(*domains) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 198

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)


193
194
195
196
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 193

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)


100
101
102
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 100

def foobara_domain?
  true
end

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



20
21
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
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 20

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



62
63
64
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 62

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

#foobara_full_domain_symbolObject



75
76
77
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 75

def foobara_full_domain_symbol
  Util.underscore_sym(foobara_full_domain_name)
end

#foobara_full_organization_nameObject



83
84
85
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 83

def foobara_full_organization_name
  foobara_organization&.foobara_full_organization_name
end

#foobara_manifestObject



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

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

  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(types:)

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

  manifest
end

#foobara_organizationObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 87

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



79
80
81
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 79

def foobara_organization_name
  foobara_organization&.foobara_organization_name
end

#foobara_register_and_deanonymize_entities(entity_names_to_attributes) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'projects/entities/projects/entity/src/extensions/domain/domain_module_extension.rb', line 89

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



13
14
15
16
# File 'projects/entities/projects/entity/src/extensions/domain/domain_module_extension.rb', line 13

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



99
100
101
102
103
104
105
106
107
# File 'projects/entities/projects/entity/src/extensions/domain/domain_module_extension.rb', line 99

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



19
20
21
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
# File 'projects/entities/projects/entity/src/extensions/domain/domain_module_extension.rb', line 19

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_type(type_symbol, *type_declaration_bits, &block) ⇒ 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 134

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

  type
end

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



7
8
9
10
11
# File 'projects/entities/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



105
106
107
108
109
110
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 105

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



112
113
114
115
116
117
118
119
120
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 112

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



128
129
130
131
132
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 128

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



122
123
124
125
126
# File 'projects/typesystem/projects/domain/src/domain_module_extension.rb', line 122

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