Class: Audiothority::Validators::Unique

Inherits:
Object
  • Object
show all
Defined in:
lib/audiothority/validators/unique.rb

Direct Known Subclasses

Album, Artist, Year

Instance Method Summary collapse

Constructor Details

#initialize(thing) ⇒ Unique

Returns a new instance of Unique.



6
7
8
# File 'lib/audiothority/validators/unique.rb', line 6

def initialize(thing)
  @thing = thing
end

Instance Method Details

#validate(tags) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/audiothority/validators/unique.rb', line 10

def validate(tags)
  items = tags.map { |t| t.send(@thing) }.uniq
  if items.one?
    Validation.new
  elsif items.compact.empty?
    Violation.new(@thing, :missing, %(missing #{@thing} field))
  elsif items.size > 1
    Violation.new(@thing, :multiple, %(multiple #{@thing}s: #{items.map(&:inspect).join(', ')}))
  end
end