Sha256: 796ee0499f06a5a4da56e16298f736f794a2c1c0adfd62a51de5dbf22c40287c

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

require "shoulda-matchers"

module Shoulda
  module Matchers
    module ActiveModel
      def validate_as_usa_telephone(attr)
        ValidateAsUsaTelephoneMatcher.new(attr)
      end

      class ValidateAsUsaTelephoneMatcher < ValidationMatcher
        def initialize(attribute)
          @attribute = attribute
        end

        def description
          "validate #{@attribute} as a valid Telephone number for United States"
        end

        def failure_message
          "expected #{@attribute} to be validated as a valid Telephone number for United States"
        end

        def matches?(subject)
          super(subject)

          disallows_invalid_value and allows_valid_value and allows_nil_value
        end

        private

        def disallows_invalid_value
          disallows_value_of("123456")
        end

        def allows_valid_value
          allows_value_of("(111)222-3333")
        end

        def allows_nil_value
          allows_value_of(nil)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validates_telephone-0.2.1 lib/validates_telephone/shoulda-matchers/validate_as_usa_telephone_matcher.rb
validates_telephone-0.2.0 lib/validates_telephone/shoulda-matchers/validate_as_usa_telephone_matcher.rb