Sha256: 7e8fc20df181e8b1c654e3d8acacf1b200baaa53f7556ef0909228f5bcf403c4
Contents?: true
Size: 1.76 KB
Versions: 4
Compression:
Stored size: 1.76 KB
Contents
# frozen_string_literal: true class FileSizeValidator < BaseValidator CHECKS = { in: :===, less_than: :<, less_than_or_equal_to: :<=, greater_than: :>, greater_than_or_equal_to: :>=, equal_to: :==, not_equal_to: :!= }.freeze def validate_each(record, attribute, value) assert_valid_checks! assign_attr_readers(record, attribute, value) valid? end private # rubocop:disable Layout/LineLength, Style/GuardClause def assert_valid_checks! if checks.empty? raise ArgumentError, "Missing atleast one check of #{CHECKS.keys.map(&:inspect).join(', ')} attribute for comparison." else assert_valid_option!(:checks, CHECKS.keys) end end # rubocop:enable Layout/LineLength, Style/GuardClause def checks options.keys & CHECKS.keys end def error_message_for(option, option_value) options[:message] || I18n.t( "errors.messages.file_size.#{option}", **error_options(option_value) ) end def error_options(option_value) if option_value.is_a?(Range) { min: option_value.min, max: option_value.max } else { count: option_value } end end def valid_attr? options.slice(*CHECKS.keys).each do |option, option_value| validate_check(option, option_value) end end def valid_size?(size, option, option_value) return false if size.nil? if option_value.is_a?(Range) option_value.public_send(CHECKS[option], size) else size.public_send(CHECKS[option], option_value) end end def validate_check(option, option_value) size = value.respond_to?(:byte_size) ? value.byte_size : value.size return if valid_size?(size, option, option_value) record.errors.add(attribute, error_message_for(option, option_value)) end end
Version data entries
4 entries across 4 versions & 1 rubygems