Class: Wordle

Inherits:
ApplicationRecord show all
Defined in:
app/models/wordle.rb

Overview

This model represents the daily Wordle plays.

Raises:

  • (ValidationError)

    if the word is missing or not 5 characters

  • (ValidationError)

    if the word is not a valid solution from the WordleDictionary

  • (ValidationError)

    if the play_date has already been set

  • (ValidationError)

    if the play_date is in the past or beyond 2 weeks into the future

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#play_dateDate

The date for which the word is to be set

Returns:

  • (Date)

    the current value of play_date



11
12
13
# File 'app/models/wordle.rb', line 11

def play_date
  @play_date
end

#skip_today_validationBoolean

Flag used to specifiy that validating play_date in the past should be skipped.

Returns:

  • (Boolean)

    the current value of skip_today_validation



11
12
13
# File 'app/models/wordle.rb', line 11

def skip_today_validation
  @skip_today_validation
end

#wordString

The word to be set as the solution for a given date

Returns:

  • (String)

    the current value of word



11
12
13
# File 'app/models/wordle.rb', line 11

def word
  @word
end

Instance Method Details

#skip_today_validation?Boolean

Flag to allow new Wordle Plays to be created in the past, used to set today’s play if a Puzzle Setter hasn’t set it beforehand

Returns:

  • (Boolean)


36
37
38
# File 'app/models/wordle.rb', line 36

def skip_today_validation?
    skip_today_validation == true
end

#valid_date_after_todayObject

Validates that the play_date is not in the past (including today)



26
27
28
# File 'app/models/wordle.rb', line 26

def valid_date_after_today
    errors.add(:play_date, "has to be in the future") if play_date <= Date.today
end

#valid_date_within_two_weeksObject

Validates that the play_date is not beyond two weeks into the future



31
32
33
# File 'app/models/wordle.rb', line 31

def valid_date_within_two_weeks
    errors.add(:play_date, "has to be within the next 2 weeks") if play_date > Date.today+14
end

#valid_solutionObject

Validates that the word exists in WordleDictionary as a valid solution



21
22
23
# File 'app/models/wordle.rb', line 21

def valid_solution
    errors.add(:word, "is not a valid wordle solution") if WordleDictionary.where(word: word, is_valid_solution: true).empty?
end