class AutomationObject::BluePrint::HashAdapter::Validators::ValidateViewPresenceOf

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

Public Class Methods

new(args) click to toggle source

@param args [Hash] arguments for the validation class

# File lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb, line 12
def initialize(args)
  @key = args.fetch :key
end

Public Instance Methods

validate(composite_object) click to toggle source

Validates the composite object and throws errors on failure @param composite_object [Object] Composite object to be tested. @return [nil] no return on exceptions on failure

# File lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb, line 19
def validate(composite_object)
  # Get the hash value from the composite object
  target_value = composite_object.hash[@key]
  target_values = target_value.is_a?(Array) ? target_value : [target_value]

  return unless target_value

  valid_views = find_views(composite_object)
  populate_errors(target_values, valid_views, composite_object)
end

Private Instance Methods

find_views(composite_object) click to toggle source

Need to traverse up the composite tree and find views

# File lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb, line 33
def find_views(composite_object)
  # Using the hash instead of the method because lower nodes will get validated
  # before composite is finished building
  return composite_object.hash[:views].keys if composite_object.hash[:views].is_a?(Hash)

  return find_views(composite_object.parent) if composite_object.parent

  []
end
populate_errors(target_values, valid_views, composite_object) click to toggle source
# File lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb, line 43
def populate_errors(target_values, valid_views, composite_object)
  target_values.each do |view|
    view = view.to_sym
    next if valid_views.include?(view)

    error_message = "Invalid View: #{view}, at: #{composite_object.location}[#{@key}]."
    error_message += " Valid Views(s): #{valid_views}"
    error_messages.push(error_message)
  end
end