blob: cb9a6e9eb2c2ac80d662584e04bf5a0244e02088 (
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!
@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
if @phone.nil? || @phone.strip.empty?
@errors << "Du må oppgi et telefonnummer"
end
if @email.nil? || @email.strip.empty?
@errors << "Du må oppgi en epostadresse"
end
@errors
end
private
def valid_postcode
@postcode && @postcode =~ /^\d{4}$/
end
end
|