Class: Codebreaker::Guess

Inherits:
ValidatableEntity show all
Defined in:
lib/app/entities/guess.rb

Constant Summary collapse

HINT =
'hint'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ValidatableEntity

#valid?

Methods included from Validator

#check_cover?, #check_include?, #check_numbers?, #check_size?

Constructor Details

#initialize(input) ⇒ Guess

Returns a new instance of Guess.



9
10
11
12
# File 'lib/app/entities/guess.rb', line 9

def initialize(input)
  super()
  @input = input
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/app/entities/guess.rb', line 5

def errors
  @errors
end

#inputObject (readonly)

Returns the value of attribute input.



5
6
7
# File 'lib/app/entities/guess.rb', line 5

def input
  @input
end

Instance Method Details

#as_array_of_numbersObject



21
22
23
# File 'lib/app/entities/guess.rb', line 21

def as_array_of_numbers
  @as_array_of_numbers ||= @input.chars.map(&:to_i)
end

#hint?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/app/entities/guess.rb', line 25

def hint?
  @input == HINT
end

#validateObject



14
15
16
17
18
19
# File 'lib/app/entities/guess.rb', line 14

def validate
  return if hint?

  @errors << I18n.t('invalid.include_error') unless check_numbers?(@input, valid_numbers)
  @errors << I18n.t('invalid.size_error') unless check_size?(@input, Game::CODE_SIZE)
end