Class: Etna::Clients::Magma::AddLinkActionValidator

Inherits:
AttributeActionValidatorBase show all
Defined in:
lib/etna/clients/magma/workflows/json_validators.rb

Instance Attribute Summary

Attributes inherited from AttributeActionValidatorBase

#action, #project_models

Attributes inherited from ValidatorBase

#errors

Instance Method Summary collapse

Methods inherited from AttributeActionValidatorBase

#action_to_attribute, #check_already_exists_in_model, #check_does_not_exist_in_model, #exists_in_magma_model?, #initialize, #validate_model_exists

Methods inherited from ValidatorBase

#check_in_set, #check_key, #check_key_empty, #check_valid_name_with_numbers, #format_errors, #initialize, #model_exists_in_project?, #name_regex_no_numbers, #name_regex_with_numbers, #nil_or_empty?, #valid?, #validate!

Constructor Details

This class inherits a constructor from Etna::Clients::Magma::AttributeActionValidatorBase

Instance Method Details

#destObject



334
335
336
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 334

def dest
  action.links.last
end

#sourceObject



330
331
332
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 330

def source
  action.links.first
end

#validateObject



338
339
340
341
342
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 338

def validate
  validate_links
  validate_both_models_exist
  validate_link_data
end

#validate_both_models_existObject



355
356
357
358
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 355

def validate_both_models_exist
  validate_model_exists(source[:model_name])
  validate_model_exists(dest[:model_name])
end


360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 360

def validate_link_data
  # Make sure the attribute names don't already exist in the models,
  #   and that the types are valid.
  link_types = Set.new([source[:type], dest[:type]])
  expected_link_types = Set.new([
      Etna::Clients::Magma::AttributeType::LINK,
      Etna::Clients::Magma::AttributeType::COLLECTION
  ])
  if link_types != expected_link_types
    @errors << "You must have one \"link\" and one \"collection\" type in the links."
  else
    check_already_exists_in_model(source[:model_name], source[:attribute_name])
    check_already_exists_in_model(dest[:model_name], dest[:attribute_name])

    @errors << "Links #{source} and #{dest} must point to each other." unless source[:model_name] == dest[:attribute_name] && source[:attribute_name] == dest[:model_name]
  end
end


344
345
346
347
348
349
350
351
352
353
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 344

def validate_links
  check_key("action #{action}", action, :links)
  @errors << "Must include two link entries, each with \"model_name\", \"attribute_name\", and \"type\"." unless action.links.length == 2
  check_key("link #{source}", source, :model_name)
  check_key("link #{source}", source, :attribute_name)
  check_key("link #{source}", source, :type)
  check_key("link #{dest}", dest, :model_name)
  check_key("link #{dest}", dest, :attribute_name)
  check_key("link #{dest}", dest, :type)
end