Class: Foobara::TypeDeclarations::Handlers::ExtendModelTypeDeclaration::ToTypeTransformer

Inherits:
Foobara::TypeDeclarations::Handlers::ExtendRegisteredTypeDeclaration::ToTypeTransformer show all
Defined in:
foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb

Instance Attribute Summary

Attributes inherited from Value::Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Instance Method Summary collapse

Methods inherited from Foobara::TypeDeclarations::Handlers::ExtendRegisteredTypeDeclaration::ToTypeTransformer

#transform, #type_class

Methods inherited from RegisteredTypeDeclaration::ToTypeTransformer

#registered_type, #transform, #type_symbol

Methods inherited from ToTypeTransformer

#always_applicable?

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods inherited from Value::Transformer

create, error_classes, foobara_manifest, subclass, #transform

Methods inherited from Value::Processor

#always_applicable?, #applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #error_context, #error_message, #error_path, foobara_manifest, #foobara_manifest, #initialize, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol

Methods included from IsManifestable

#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches

Constructor Details

This class inherits a constructor from Foobara::Value::Processor

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Instance Method Details

#existing_class_from_same_namespace_root(model_class_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb', line 6

def existing_class_from_same_namespace_root(model_class_name)
  if Object.const_defined?(model_class_name) && Object.const_get(model_class_name).is_a?(::Class)
    existing_class = Object.const_get(model_class_name)

    # If we are operating in some other namespace tree then we don't want to use the non-anonymous class
    if Domain.current == GlobalDomain || Domain.current.foobara_root_namespace == Foobara::Namespace.global
      return existing_class
    end

    model_type = existing_class.model_type

    if model_type
      if model_type.foobara_root_namespace == Foobara::Namespace.current.foobara_root_namespace
        # TODO: test this code path
        # :nocov:
        existing_class
        # :nocov:
      end
    end
  else
    existing_type = Domain.current.foobara_lookup_type(
      model_class_name,
      mode: Namespace::LookupMode::ABSOLUTE
    )

    existing_type&.target_class
  end
end

#non_processor_keysObject

TODO: create declaration validator for name and the others TODO: seems like a smell that we don’t have processors for these?



62
63
64
65
66
67
68
69
70
71
72
73
# File 'foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb', line 62

def non_processor_keys
  [
    :name,
    :model_class,
    :model_base_class,
    :model_module,
    :attributes_declaration,
    :delegates,
    :private,
    *super
  ]
end

#process_valueObject



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
# File 'foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb', line 75

def process_value(...)
  super.tap do |outcome|
    if outcome.success?
      type = outcome.result

      handler = handler_for_class(ExtendAttributesTypeDeclaration)
      attributes_type_declaration = type.declaration_data[:attributes_declaration]

      type.element_types = handler.process_value!(attributes_type_declaration)

      model_class = type.target_class
      existing_model_type = model_class.model_type

      if existing_model_type
        # :nocov:
        raise "Did not expect #{type.declaration_data[:name]} to already exist"
        # :nocov:
      else
        model_class.model_type = type

        # this is a fairly complex way of making sure that we are getting the domain from the
        # current namespace tree which might not be the case if we're in a command connector
        # namespace
        domain = model_class.domain
        domain_name = domain.scoped_full_name
        root_namespace = Namespace.current.foobara_root_namespace
        domain = root_namespace.foobara_lookup_domain(domain_name)

        type_symbol = type.declaration_data[:name]
        type.type_symbol = type_symbol.to_sym

        model_class.description type.declaration_data[:description]

        if domain.foobara_type_registered?(type_symbol, mode: Namespace::LookupMode::ABSOLUTE)
          existing_type = domain.foobara_lookup_type(type_symbol, mode: Namespace::LookupMode::ABSOLUTE)
          domain.foobara_unregister(existing_type)
        end

        domain.foobara_register_model(model_class)

        if type.declaration_data[:delegates]
          model_class.delegate_attributes type.declaration_data[:delegates]
        end

        if type.declaration_data[:private]
          model_class.private_attributes type.declaration_data[:private]
        end
      end
    end
  end
end

#target_classes(strict_type_declaration) ⇒ Object

TODO: make declaration validator for model_class and model_base_class



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb', line 36

def target_classes(strict_type_declaration)
  model_class_name = strict_type_declaration[:model_class]

  existing_class = existing_class_from_same_namespace_root(model_class_name)

  if existing_class
    return existing_class
  end

  base_class_name = strict_type_declaration[:model_base_class]

  base_class = existing_class_from_same_namespace_root(base_class_name)
  base_class ||= lookup_type(base_class_name)&.target_class
  # If we make it here, it's a real base class like Foobara::Entity
  base_class ||= Object.const_get(base_class_name)

  base_class.subclass(name: model_class_name)
end

#type_name(strict_type_declaration) ⇒ Object

TODO: must explode if name missing…



56
57
58
# File 'foobara-0.0.110/projects/model/src/extensions/type_declarations/handlers/extend_model_type_declaration/to_type_transformer.rb', line 56

def type_name(strict_type_declaration)
  strict_type_declaration[:name]
end