File: phone.rb

package info (click to toggle)
ruby-valid 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ruby: 309; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 780 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Validation
  module Rule
    # Phone rule
    class Phone
      # params can be any of the following:
      #
      #  - :format - the phone number format
      #
      #  Example:
      #
      #  {:format => :america}
      def initialize(params = {:format => :america})
        @params = params
      end

      # returns the params given in the constructor
      def params
        @params
      end

      # determines if value is valid according to the constructor params
      def valid_value?(value)
        send(@params[:format], value)
      end

      def error_key
        :phone
      end

      protected

      def america(value)
        digits = value.gsub(/\D/, '').split(//)

        digits.length == 10 || digits.length == 11
      end
    end
  end
end