Class: SportsManager::TournamentDay::Validator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sports_manager/tournament_day/validator.rb

Overview

Public: Validates tournament day’s date and time range

Constant Summary collapse

DateParsingError =
Class.new(StandardError) do
  def initialize(date)
    message = "It is not possible to parse Date: #{date}"
    super(message)
  end
end
InvalidHour =
Class.new(StandardError) do
  def initialize(start_hour, end_hour)
    message = 'start_hour and end_hour must be between 0 and 23. ' \
              "start_hour: #{start_hour}, end_hour: #{end_hour}"
    super(message)
  end
end
MIN_HOUR =
0
MAX_HOUR =
23

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tournament_day) ⇒ Validator

Returns a new instance of Validator.



31
32
33
# File 'lib/sports_manager/tournament_day/validator.rb', line 31

def initialize(tournament_day)
  @tournament_day = tournament_day
end

Instance Attribute Details

#tournament_dayObject (readonly)

Returns the value of attribute tournament_day.



27
28
29
# File 'lib/sports_manager/tournament_day/validator.rb', line 27

def tournament_day
  @tournament_day
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sports_manager/tournament_day/validator.rb', line 42

def valid?
  parseable_date? && in_day_range?
end

#validate!Object

Raises:



35
36
37
38
39
40
# File 'lib/sports_manager/tournament_day/validator.rb', line 35

def validate!
  return true if valid?
  raise DateParsingError, date unless parseable_date?

  raise InvalidHour.new(start_hour, end_hour)
end