Class: Drawy::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/drawy/validator.rb

Class Method Summary collapse

Class Method Details

.couple_plus_one?(people, couples) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/drawy/validator.rb', line 25

def couple_plus_one?(people, couples)
  return true if people.size == 3 && couples.keys.size == 1
  false
end

.validate_people_formatObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/drawy/validator.rb', line 11

def validate_people_format
  people = Drawy::Engine.people
  if people.nil? || people.empty? || people.size < 2
    raise Drawy::ValidationError, 'You must register at least 2 people or couples before drawing'
  elsif people.size == 2 && Drawy::Engine.is_couple?(people[0], people[1])
    raise Drawy::ValidationError, 'The current pool only contains a couple, please add more people'
  # A couple plus one person creates a triangle making the distribution impossible given the rules
  # We either end up with one person in couple buying a gift for its partner or the 3rd
  # person receiving 2 gifts and someone receiving none
  elsif couple_plus_one?(people, Drawy::Engine.couples)
    raise Drawy::ValidationError, 'The current pool only contains a couple and one person, please add more people'
  end
end

.validate_uniqueness_of(*args) ⇒ Object



4
5
6
7
8
9
# File 'lib/drawy/validator.rb', line 4

def validate_uniqueness_of(*args)
  return if Drawy::Engine.people.nil?
  faulty_names = Drawy::Engine.people & args
  return if faulty_names.empty?
  raise Drawy::ValidationError, "The following name(s) already exist(s): #{faulty_names}"
end