Sha256: 33a80760069558287e202edc26e10e9c6ad31b7077748f7a78e94399a28f1c0e
Contents?: true
Size: 1.17 KB
Versions: 2
Compression:
Stored size: 1.17 KB
Contents
class SubsetValidator < ActiveModel::EachValidator ERROR_MESSAGE = "An argument must be supplied for the :of option " << "of the configuration hash and must evaluate to an array or a set" def check_validity! if options[:of].respond_to?(:call) return elsif options[:of].is_a?(Array) || options[:of].is_a?(Set) unless ActiveSubsetValidator.is_a_set? options[:of] raise ArgumentError, ERROR_MESSAGE end else raise ArgumentError, ERROR_MESSAGE end end def validate_each(record, attribute, value) message = options[:message] ? options[:message] : "is not a subset of the list" allow_nil = options[:allow_nil] === false ? false : true if options[:of].respond_to?(:call) superset = options[:of].call(record) unless ActiveSubsetValidator.is_a_set? superset raise ArgumentError, ERROR_MESSAGE end else superset = options[:of] end if value.nil? unless allow_nil record.errors[attribute] << "cannot be nil" end else unless ActiveSubsetValidator.set_difference(value, superset).empty? record.errors[attribute] << message end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_subset_validator-1.0.0 | lib/active_subset_validator/subset_validator.rb |
active_subset_validator-0.0.1 | lib/active_subset_validator/subset_validator.rb |