blob: 4df29e80c10318c517d16c9b01ab99b34e4f57d1 (
plain) (
tree)
|
|
class Contact
attr_reader :name, :street, :postcode, :city, :phone, :email
def initialize(params = nil)
if params
@name = params['name']
@street = params['street']
@postcode = params['postcode']
@city = params['city']
@phone = params['phone']
@email = params['email']
end
end
def addr
"#{@street}, #{@postcode} #{@city}"
end
def valid?
validate! unless @errors
@errors.length == 0
end
def validate!
@errors = []
if @name.nil? || @name.strip.empty?
@errors << "Du må oppgi en kontaktperson"
end
unless valid_postcode
@errors << "Du må oppgi et gyldig postnr."
end
if @city.nil? || @city.strip.empty?
@errors << "Du må oppgi poststed."
end
unless valid_phone?
@errors << "Telefonnummer mangler eller er ugyldig"
end
unless valid_email?
@errors << "E-postadresse mangler eller er ugyldig"
end
@errors
end
private
def valid_postcode
@postcode && @postcode =~ /^\d{4}$/
end
def valid_email?
@email && @email =~ /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]{1,63}\.)+[A-Z]{2,}$/i
end
def valid_phone?
@phone && @phone =~ /^\+*[0-9\- ]{8,}$/
end
end
|