class Audiothority::Validators::Unique

Public Class Methods

new(thing) click to toggle source
# File lib/audiothority/validators/unique.rb, line 6
def initialize(thing)
  @thing = thing
end

Public Instance Methods

validate(tags) click to toggle source
# 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