Class: AutomationObject::BluePrint::HashAdapter::Validators::ValidatePresenceOf

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

Overview

Validates tests the a key exists on a composite hash

Instance Attribute Summary

Attributes inherited from Validate

#error_messages

Instance Method Summary collapse

Methods inherited from Validate

#valid?

Constructor Details

#initialize(args) ⇒ ValidatePresenceOf

Returns a new instance of ValidatePresenceOf

Parameters:

  • args (Hash)

    arguments for the validation class



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

def initialize(args)
  @key = args.fetch :key

  options = args.fetch :args, {}
  options = options.is_a?(Hash) ? options : {}
  @unless_presence_of = options.fetch :unless_presence_of, nil
end

Instance Method Details

#validate(composite_object) ⇒ nil

Validates the composite object and throws errors on failure

Parameters:

  • composite_object (Object)

    Composite object to be tested.

Returns:

  • (nil)

    no return on exceptions on failure



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

def validate(composite_object)
  # Get the hash value from the composite object
  return if composite_object.hash.key?(@key)

  # Do unless_presence_of check
  if @unless_presence_of
    return if composite_object.hash.key?(@unless_presence_of)
  end

  error_messages.push("Required Key Missing: #{@key}, at: #{composite_object.location}.")
end