Sha256: faaf5e7a0e27036f7914b0da1e483ac74b66fb1eb0df2e7e1b21665240d096b6

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'shoulda-matchers'

module Shoulda
  module Matchers
    module ActiveModel
      def validate_cns(attr = :cns)
        ValidateCnsMatcher.new(attr)
      end

      class ValidateCnsMatcher < ValidationMatcher
        ALLOW_VALUES = [
          '166947669770008',
          222_491_445_220_008,
          '736 3347 8546 0000',
          '807769250350009',
          954_180_214_890_002
        ].freeze

        DISALLOW_VALUES = %w[12345678912314 1234567891234516].freeze

        def initialize(attribute)
          super(attribute)
          @options = {}
        end

        def description
          message = 'requires a valid CNS'

          message += ' included blank' if expects_to_allow_blank?

          message
        end

        def failure_message
          'does not require a valid CNS'
        end

        def matches?(subject)
          super(subject)

          ALLOW_VALUES.each { |value| allows_value_of(value) }
        end

        def does_not_match?(subject)
          super(subject)

          DISALLOW_VALUES.each { |value| disallows_value_of(value) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cns_brazil-1.0.4 lib/cns_brazil/validate_cns_matcher.rb