Class: Foobara::RemoteImports::ImportCommand

Inherits:
Command
  • Object
show all
Includes:
ImportBase
Defined in:
foobara-remote-imports-1.2.0/src/foobara/remote_imports/import_command.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary

Attributes included from ImportBase

#manifest_data, #manifest_to_import, #manifests_to_import

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome, #raw_result

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods included from ImportBase

#already_exists?, #already_imported, #cache_file_path, #cache_key, #cache_manifest, #cached?, #determine_manifests_to_import, #execute, #filter_manifests_to_import, #import_objects_from_manifests, #imported_objects, included, #load_cached_manifest, #load_manifest, #load_manifest_from_url, #root_manifest, #should_cache?, #validate, #validate_manifest

Methods inherited from Command

install!

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Callbacks

#state_machine_callback_registry

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #success?

Methods included from CommandPatternImplementation::Concerns::Errors

#has_errors?, #initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from CommandPatternImplementation::Concerns::ResultType

#result_type

Methods included from CommandPatternImplementation::Concerns::InputsType

#inputs_type

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Method Details

#build_commandObject



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
# File 'src/foobara/remote_imports/import_command.rb', line 96

def build_command
  url_base = root_manifest.["url"]
  url_base = URI.parse(url_base)
  url_base = URI::Generic.new(url_base.scheme, url_base.userinfo, url_base.host, url_base.port,
                              nil, nil, nil, nil, nil)
  url_base = url_base.to_s

  domain = Namespace.global.foobara_lookup_domain!(
    manifest_to_import.domain.scoped_full_path,
    mode: Namespace::LookupMode::ABSOLUTE_SINGLE_NAMESPACE
  )

  inputs_type = nil
  result_type = nil

  TypeDeclarations.strict_stringified do
    inputs_type = domain.foobara_type_from_declaration(manifest_to_import.inputs_type.relevant_manifest)
    result_type = domain.foobara_type_from_declaration(manifest_to_import.result_type.relevant_manifest)
  end

  subclass_args = {
    url_base:,
    description: manifest_to_import.description,
    inputs: inputs_type,
    result: result_type,
    possible_errors: manifest_to_import.possible_errors,
    name: manifest_to_import.reference,
    base: determine_base_command_class
  }

  if determine_base_command_class == AuthenticatedRemoteCommand
    subclass_args.merge!(auth_header:)
  end

  determine_base_command_class.subclass(**subclass_args)
end

#build_errorsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'src/foobara/remote_imports/import_command.rb', line 76

def build_errors
  manifest_to_import.possible_errors.each_value do |possible_error|
    error = possible_error.error

    existing_error = Foobara.foobara_root_namespace.foobara_lookup_error(
      error.reference,
      mode: Namespace::LookupMode::ABSOLUTE
    )

    next if existing_error

    run_subcommand!(
      ImportError,
      raw_manifest: manifest_data,
      to_import: error.reference,
      already_imported:
    )
  end
end

#determine_base_command_classObject



133
134
135
136
137
138
139
140
141
142
# File 'src/foobara/remote_imports/import_command.rb', line 133

def determine_base_command_class
  @determine_base_command_class ||= if base_command_class
                                      base_command_class
                                    elsif manifest_to_import.requires_authentication? &&
                                          auth_header
                                      AuthenticatedRemoteCommand
                                    else
                                      RemoteCommand
                                    end
end

#find_manifests_to_importObject



40
41
42
# File 'src/foobara/remote_imports/import_command.rb', line 40

def find_manifests_to_import
  root_manifest.commands
end

#import_object_from_manifestObject



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
# File 'src/foobara/remote_imports/import_command.rb', line 44

def import_object_from_manifest
  existing_command = Foobara.foobara_root_namespace.foobara_lookup_command(
    manifest_to_import.reference,
    mode: Namespace::LookupMode::ABSOLUTE
  )

  return if existing_command

  domain_manifest = manifest_to_import.domain

  run_subcommand!(
    ImportDomain,
    raw_manifest: manifest_data,
    to_import: domain_manifest.reference,
    already_imported:
  )

  Util.make_class_p(manifest_to_import.reference, determine_base_command_class)

  manifest_to_import.types_depended_on.each do |type|
    run_subcommand!(
      ImportType,
      raw_manifest: manifest_data,
      to_import: type.reference,
      already_imported:
    )
  end

  build_errors
  build_command
end

#load_manifest_headersObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'src/foobara/remote_imports/import_command.rb', line 21

def load_manifest_headers
  if manifest_requires_auth
    key = auth_header.first
    value = auth_header.last

    if value.is_a?(Proc)
      value = if value.lambda? && value.arity == 0
                value.call
              else
                value.call(self)
              end
    end

    super.merge(key => value)
  else
    super
  end
end