Sha256: 7f571c68fd16e8896fb05b627c170f709b1bf40a8a863f911e20408d15221feb

Contents?: true

Size: 834 Bytes

Versions: 7

Compression:

Stored size: 834 Bytes

Contents

class CodeMaker
  include Validation

  attr_reader :verified_code, :code

  CORRECT_ANSWER = ['+', '+', '+', '+'].freeze

  def initialize
    @code = generate_random_code
    @verified_code = []
  end

  def verify(user_code)
    return format(user_code) if validate_code(user_code)

    raise CustomErrors::CodeTypeError
  end

  def check?
    verified_code == CORRECT_ANSWER
  end

  private

  def generate_random_code
    4.times.map { rand(1..6) }
  end

  def format(user_code)
    @verified_code = CodeFormatter.call(code.dup, code_to_array(user_code))
  end

  def code_to_array(code)
    code.to_s.chars.map(&:to_i)
  end

  def validate_code(code)
    code = code_to_array(code)
    validate_max_length(code, 4) &&
      validate_min_length(code, 4) &&
      code.all? { |number| validate_range(number, 1, 6) }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
codebreaker-engine-0.1.7 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.6 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.5 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.4 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.3 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.2 lib/codebreaker/codemaker.rb
codebreaker-engine-0.1.1 lib/codebreaker/codemaker.rb