summaryrefslogblamecommitdiffstats
path: root/lib/contact.rb
blob: cb9a6e9eb2c2ac80d662584e04bf5a0244e02088 (plain) (tree)
1
2
3
4
5
6
7
8
9
             
                                                              
 


                              


                                    


                              
     
 



                                       




                       
               
                
                                       
                                                

       

                                                 


                                       
                                         


                                         
                                                


                                         
                                               

       






                                       
     
   
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