Sha256: 98f588d537a1c714dd91ab9edd26f89e2b6994804d90ae3d77be73d322aa23ab

Contents?: true

Size: 787 Bytes

Versions: 41

Compression:

Stored size: 787 Bytes

Contents

# frozen_string_literal: true

class MiniDefender::Rules::MaxDigits < MiniDefender::Rule
  def initialize(limit)
    raise ArgumentError, 'Limit must be a string.' unless limit.is_a?(Integer)

    @limit = limit
  end

  def self.signature
    'max_digits'
  end

  def self.make(args)
    raise ArgumentError, 'Expected at least one argument for max_digits.' unless args.length == 1

    new(args[0].to_i)
  end

  def passes?(attribute, value, validator)
    @integers = valid = value.is_a?(Integer) || value.is_a?(String) && value.match?(/^\d+$/)
    valid && value.to_s.length <= @limit
  end

  def message(attribute, value, validator)
    if @integers
      'The field should only contain digits.'
    else
      "The field should have at most #{@limit} digits."
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
mini_defender-0.6.8 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.7 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.5 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.4 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.3 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.2 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.1 lib/mini_defender/rules/max_digits.rb
mini_defender-0.6.0 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.8 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.7 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.6 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.5 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.4 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.3 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.2 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.1 lib/mini_defender/rules/max_digits.rb
mini_defender-0.5.0 lib/mini_defender/rules/max_digits.rb
mini_defender-0.4.1 lib/mini_defender/rules/max_digits.rb
mini_defender-0.4.0 lib/mini_defender/rules/max_digits.rb
mini_defender-0.3.14 lib/mini_defender/rules/max_digits.rb