Sha256: 43c9218b75d8b69f1f114658187491a6dc5526709cc43b33dfce93c40a03881b

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 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.send(CHECKS[option], size)
    else
      size.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

7 entries across 7 versions & 1 rubygems

Version Path
lite-validators-1.5.0 lib/lite/validators/file_size_validator.rb
lite-validators-1.4.0 lib/lite/validators/file_size_validator.rb
lite-validators-1.3.2 lib/lite/validators/file_size_validator.rb
lite-validators-1.3.1 lib/lite/validators/file_size_validator.rb
lite-validators-1.3.0 lib/lite/validators/file_size_validator.rb
lite-validators-1.2.0 lib/lite/validators/file_size_validator.rb
lite-validators-1.1.2 lib/lite/validators/file_size_validator.rb