Sha256: 10f1f69ac28da51db0996e625b5adda4a898f3c0da874715c69220c5a431ee67

Contents?: true

Size: 463 Bytes

Versions: 2

Compression:

Stored size: 463 Bytes

Contents

class Telephone
  def initialize(number)
    @match = number =~ /^(\(?\d{2}\)?)(\d{4})-?(\d{4})$/
    @number = number
    @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!
    @cleaned_number =~ /^(\d{2})(\d{4})(\d{4})$/
    @number = "(#{$1})#{$2}-#{$3}"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
validates_telephone-0.1.0 lib/validates_telephone/telephone.rb
validates_telephone-0.0.1 lib/validates_telephone/telephone.rb