Sha256: 0141fe9bd5c35871d32ebfa5ec5d0387b5774db209838fcba633b42e7fe9627e

Contents?: true

Size: 729 Bytes

Versions: 2

Compression:

Stored size: 729 Bytes

Contents

class Telephone
  def initialize(number, locale)
    @number = number
    @locale = locale
    if @locale == :br
      @match = number =~ /^(\(?\d{2}\)?)(\d{4})-?(\d{4})$/
    else
      @match = number =~ /^(\(?[^2-9]\d{2}\)?)(\d{3})-?(\d{4})$/
    end
    @cleaned_number = $1.nil? ? nil : @number.gsub(/[\(\)-]/, "")
    format_number! if @cleaned_number
  end

  def valid?
    return false unless @number.nil? or @match
    true
  end

  def number
    @number
  end

  private

  def format_number!
    if @locale == :br
      @cleaned_number =~ /^(\d{2})(\d{4})(\d{4})$/
      @number = "(#{$1})#{$2}-#{$3}"
    else
      @cleaned_number =~ /^(\d{3})(\d{3})(\d{4})$/
      @number = "(#{$1})#{$2}-#{$3}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validates_telephone-0.2.1 lib/validates_telephone/telephone.rb
validates_telephone-0.2.0 lib/validates_telephone/telephone.rb