Sha256: c3ece5fda8beda4cafbf9fde783d392d38d9d0686efcdd0a3d3b9a4a5fcf841e

Contents?: true

Size: 1.65 KB

Versions: 22

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Cocina
  module Models
    module Validators
      # Validates that only a single CatalogLink has refresh set to true
      class CatalogLinksValidator
        MAX_REFRESH_CATALOG_LINKS = 1

        def self.validate(clazz, attributes)
          new(clazz, attributes).validate
        end

        def initialize(clazz, attributes)
          @clazz = clazz
          @attributes = attributes
        end

        def validate
          return unless meets_preconditions?

          validate_catalog('symphony')
          validate_catalog('folio')
        end

        private

        attr_reader :clazz, :attributes

        def validate_catalog(catalog)
          refresh_catalog_links = refresh_catalog_links_for(catalog)

          return if refresh_catalog_links.length <= MAX_REFRESH_CATALOG_LINKS

          raise ValidationError, "Multiple catalog links have 'refresh' property set to true " \
                                 "(only one allowed) #{refresh_catalog_links}"
        end

        def catalog_links
          @catalog_links ||= Array(attributes.dig(:identification, :catalogLinks))
        end

        def meets_preconditions?
          (dro? || collection?) && catalog_links.any?
        end

        def refresh_catalog_links_for(catalog)
          catalog_links.select { |catalog_link| catalog_link[:catalog] == catalog && catalog_link[:refresh] }
        end

        def dro?
          (clazz::TYPES & DRO::TYPES).any?
        rescue NameError
          false
        end

        def collection?
          (clazz::TYPES & Collection::TYPES).any?
        rescue NameError
          false
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
cocina-models-0.99.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.99.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.98.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.98.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.96.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.95.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.95.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.94.2 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.94.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.94.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.93.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.93.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.92.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.91.4 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.91.3 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.91.2 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.91.1 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.91.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.90.0 lib/cocina/models/validators/catalog_links_validator.rb
cocina-models-0.89.1 lib/cocina/models/validators/catalog_links_validator.rb