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





                                                                                    
                       
 
                              
                          
                 
               






                                     
                                        


                                               



                                      




            

                           


               
                
 



                                       

                                     

                                               

       
 

                               
     



                                                 
   
require_relative 'contact'
require_relative 'member'
require_relative 'song'

class Band
  attr_reader :name, :city, :website, :label, :short_bio, :contact, :members, :songs
  attr_accessor :errors

  def initialize(params = nil)
    @contact = Contact.new
    @members = ""
    @songs = []

    if params
      @name = params['name']
      @city = params['city']
      @website = params['website']
      @label = params['label']
      @short_bio = params['shortbio']
      @members = params['members'] || ""

      @contact = Contact.new(params['contact'])

      if params['songs']
        params['songs'].each do |k, s|
          @songs << Song.new(s)
        end
      end
    end
  end

  def valid?
    validate! unless errors
    !has_errors?
  end

  def validate!
    @errors = []

    if @name.nil? || @name.strip.empty?
      errors << "Bandnavn mangler"
    end

    errors.concat(@contact.validate!)

    if @songs.length <= 0
      errors << "Du må ha med minst én låt!"
    end
  end

  def has_errors?
    errors && errors.length > 0
  end

  def name_and_city
    city.empty? ? name : name + " (" + city + ")"
  end
end