Sha256: 1a0cb798667b89083423683536c90b37f34b46c1764aeda7cb2c9b7784b415cc

Contents?: true

Size: 790 Bytes

Versions: 2

Compression:

Stored size: 790 Bytes

Contents

#
# SingleCardinalityValidator - validates that an enumerator value has size 0 or 1
#
#    validates :myattr, single_cardinality: true
#    validates_single_cardinality_of :myattr
#
# Blank and nil values are considered valid (even without :allow_blank or :allow_nil
# validator options).
#
module Hydra
  module Validations

    class SingleCardinalityValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        if value.respond_to?(:each)
          record.errors.add(attribute, "can't have more than one value") if value.size > 1
        end
      end
    end

    module HelperMethods
      def validates_single_cardinality_of *attr_names
        validates_with SingleCardinalityValidator, _merge_attributes(attr_names)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hydra-validations-0.2.1 lib/hydra/validations/single_cardinality_validator.rb
hydra-validations-0.2.0 lib/hydra/validations/single_cardinality_validator.rb