summaryrefslogtreecommitdiffstats
path: root/lib/band.rb
blob: 8cd353588b6dba4736f9977b5d26307216a4d9cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require_relative 'contact'
require_relative 'member'
require_relative 'song'

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

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

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

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

      if params['members']
        params['members'].each do |k, m|
          @members << Member.new(m)
        end
      end

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

  def valid?
    validate!
    errors.length == 0
  end

  def validate!
    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.length > 0
  end
end