Module: CodebreakerVolkova::Validator

Includes:
Errors
Included in:
Game
Defined in:
lib/codebreaker_volkova/validation/validator.rb

Constant Summary collapse

VALID_NAME_LENGTH =
(3..20).freeze
VALID_NUMBERS =
(1..6).freeze
VALID_GUESS_LENGTH =
4
LEVEL_OF_GAME =
%w[easy medium hell].freeze

Instance Method Summary collapse

Instance Method Details

#check_constituents_difficulty?(difficulty) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



40
41
42
# File 'lib/codebreaker_volkova/validation/validator.rb', line 40

def check_constituents_difficulty?(difficulty)
  raise DifficultyError unless LEVEL_OF_GAME.include? difficulty
end

#check_constituents_guess?(guess) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



34
35
36
37
38
# File 'lib/codebreaker_volkova/validation/validator.rb', line 34

def check_constituents_guess?(guess)
  raise GuessError unless guess.each_char.map(&:to_i).all? do |num|
    VALID_NUMBERS.include?(num)
  end
end

#check_length_guess?(guess) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



30
31
32
# File 'lib/codebreaker_volkova/validation/validator.rb', line 30

def check_length_guess?(guess)
  raise GuessLengthError unless guess.length == VALID_GUESS_LENGTH
end

#check_length_name?(arg) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



26
27
28
# File 'lib/codebreaker_volkova/validation/validator.rb', line 26

def check_length_name?(arg)
  raise UserNameError unless VALID_NAME_LENGTH.include?(arg.length)
end

#instance?(main_class, *args) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/codebreaker_volkova/validation/validator.rb', line 19

def instance?(main_class, *args)
  args.each do |object|
    raise NilStringError if object.nil?
    raise WrongClassError unless object.instance_of?(main_class)
  end
end

#not_empty?(*args) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
# File 'lib/codebreaker_volkova/validation/validator.rb', line 12

def not_empty?(*args)
  args.each do |arg|
    raise NilStringError if arg.nil?
    raise EmptyStringError if arg.empty?
  end
end