lib/validates_telephone/telephone.rb in validates_telephone-0.1.0 vs lib/validates_telephone/telephone.rb in validates_telephone-0.2.0
- old
+ new
@@ -1,9 +1,14 @@
class Telephone
- def initialize(number)
- @match = number =~ /^(\(?\d{2}\)?)(\d{4})-?(\d{4})$/
+ 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?
@@ -16,9 +21,14 @@
end
private
def format_number!
- @cleaned_number =~ /^(\d{2})(\d{4})(\d{4})$/
- @number = "(#{$1})#{$2}-#{$3}"
+ 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
\ No newline at end of file