Class: AutomationObject::BluePrint::HashAdapter::Validators::ValidateInstanceOf

Inherits:
Validate
  • Object
show all
Defined in:
lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_instance_of.rb

Overview

Validator that tests that a element is defined when it is called elsewhere through a hook

Instance Attribute Summary

Attributes inherited from Validate

#error_messages

Instance Method Summary collapse

Methods inherited from Validate

#valid?

Constructor Details

#initialize(args) ⇒ ValidateInstanceOf

Returns a new instance of ValidateInstanceOf

Parameters:

  • args (Hash)

    arguments for the validation class



12
13
14
15
16
# File 'lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_instance_of.rb', line 12

def initialize(args)
  @key = args.fetch :key
  # Convert to array of instances for consistency
  @should_be_instances_of = args.fetch(:args).is_a?(Array) ? args.fetch(:args) : [args.fetch(:args)]
end

Instance Method Details

#validate(composite_object) ⇒ nil

Validates the composite object and adds errors on failure

Parameters:

Returns:

  • (nil)

    no return on exceptions on failure



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_instance_of.rb', line 21

def validate(composite_object)
  # Get the hash value from the composite object
  target_value = composite_object.hash[@key]

  # Skip empty or non-existent
  return unless target_value

  return if @should_be_instances_of.any? { |should_instance| target_value.is_a?(should_instance) }

  error_message = "Invalid Type: #{target_value.class}, at: #{composite_object.location}[#{@key}]."
  error_message += " Allowed Type(s): #{@should_be_instances_of}"

  error_messages.push(error_message)
end