Sha256: 94752ce9bc2327f0b406c77fb77d1672f0baa46d97fc50e5b9be67a4e95515a6

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'shoulda-matchers'
require 'active_support/core_ext/array/wrap'

module Shoulda
  module Matchers
    module ActiveModel
      def require_a_valid_telephone(attr = :telephone)
        RequireAValidTelephoneMatcher.new(attr)
      end

      class RequireAValidTelephoneMatcher< ValidationMatcher
        def initialize(attribute)
          @attribute = attribute
          @options = {}
        end

        def from(locale)
          @options[:locale] = locale
          self
        end

        def description
          "require #{@attribute} to be a valid telephone number"
        end

        def matches?(subject)
          super(subject)

          disallows_invalid_value and allows_valid_value
        end

        private

        def disallows_invalid_value
          return (disallows_value_of("123456") and disallows_value_of("(11)2222-3333")) if @options[:locale] == :usa
          return (disallows_value_of("123456") and disallows_value_of("(111)222-3333") and disallows_value_of("(11)81111-1111")) if @options[:locale] == :br
          disallows_value_of("123456")
        end

        def allows_valid_value
          return allows_value_of("(111)222-3333") if @options[:locale] == :usa
          return (allows_value_of("(11)2222-3333") and allows_value_of("(11)91111-1111")) if @options[:locale] == :br
          allows_value_of("(111)222-3333") and allows_value_of("(11)2222-3333")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validates_telephone-2.0.2 lib/validates_telephone/require_a_valid_telephone_matcher.rb
validates_telephone-2.0.1 lib/validates_telephone/require_a_valid_telephone_matcher.rb
validates_telephone-2.0.0 lib/require_a_valid_telephone_matcher.rb