summaryrefslogtreecommitdiffstats
path: root/lib/band.rb
blob: 4e1ca81b404049a00aee1706b6c04561eeac5182 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Band registration form for Norsk Urskog Metal Sampler
# Copyright (C) 2015-2018  Harald Eilersen <haraldei@anduin.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

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