Class: DS::Manifest::BaseIdValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/manifest/base_id_validator.rb

Overview

A BaseIdValidator is a base class for a cacheable ID validator for sources. The validator is responsible for opening and caching source files and dtermining that one record is found for each source id at the specified id_location in the parsed source.

The motivation for this class is to handle ID validation for source types that can have multiple records per source file, saving the time required to parse the source file for each check.

Concrete subclasses of BaseIdValidator must implement

- +#locate_record+, required this class

Direct Known Subclasses

DsCsvIdValidator, SimpleXmlIdValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ void

Create a new ID Validator

Parameters:



30
31
32
33
# File 'lib/ds/manifest/base_id_validator.rb', line 30

def initialize source
  @source = source
  @errors = []
end

Instance Attribute Details

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



22
23
24
# File 'lib/ds/manifest/base_id_validator.rb', line 22

def errors
  @errors
end

#source ⇒ Object (readonly)

Returns the value of attribute source.



23
24
25
# File 'lib/ds/manifest/base_id_validator.rb', line 23

def source
  @source
end

Instance Method Details

#add_error(message) ⇒ Object



71
72
73
# File 'lib/ds/manifest/base_id_validator.rb', line 71

def add_error message
  @errors << message
end

#handle_count_error(count, inst_id, location_in_source) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ds/manifest/base_id_validator.rb', line 60

def handle_count_error count, inst_id, location_in_source
  return if count == 1

  if count > 1
    add_error "ERROR: Multiple records (#{count}) found for id: #{inst_id} (location: #{location_in_source})"
  elsif count == 0
    add_error "ERROR: No records found for id: #{inst_id} (location: #{location_in_source})"
  end
  nil
end

#locate_record(source_path, id, id_location) ⇒ Array<Object>

Locates a record based on the given source path, ID, and ID location.

Parameters:

  • source_path (String) —

    the path to the source file

  • id (String) —

    the ID of the record

  • id_location (String) —

    the location of the ID within the record

Returns:

  • (Array<Object>) —

    an array of objects for each record

Raises:

  • (NotImplementedError) —

    this method is not implemented and should be overridden



56
57
58
# File 'lib/ds/manifest/base_id_validator.rb', line 56

def locate_record source_path, id, id_location
  raise NotImplementedError
end

#valid?(file_path, id, id_location) ⇒ Boolean

Checks if the given file path, id, and id location are valid.

Parameters:

  • file_path (String) —

    The path to the file.

  • id (String) —

    The id to check.

  • id_location (String) —

    The location of the id.

Returns:

  • (Boolean) —

    Returns true if the records size is equal to 1, false otherwise.



41
42
43
44
45
46
47
# File 'lib/ds/manifest/base_id_validator.rb', line 41

def valid? file_path, id, id_location
  records = locate_record file_path, id, id_location

  return true if records.size == 1
  handle_count_error records.size, id, id_location
  false
end