Class: Edoxen::VenueValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/edoxen/venue_validator.rb,
sig/edoxen.rbs

Overview

VenueValidator — validates Venue instances using the unlocodes and iata gems. Returns a list of errors; empty list means valid.

For a Venue with kind: "physical", validates that any populated unlocode and iata_code exist in the canonical registries.

valid? and validate are pure queries — they do not mutate the venue. Callers who want city/country_code back-filled from the UN/LOCODE registry use populate_from_registry!, a separate, explicitly-mutating method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(venue) ⇒ VenueValidator

Returns a new instance of VenueValidator.

Parameters:



17
18
19
20
# File 'lib/edoxen/venue_validator.rb', line 17

def initialize(venue)
  @venue = venue
  @errors = []
end

Instance Attribute Details

#errorsArray[String] (readonly)

Returns the value of attribute errors.

Returns:

  • (Array[String])


15
16
17
# File 'lib/edoxen/venue_validator.rb', line 15

def errors
  @errors
end

#venueVenue (readonly)

Returns the value of attribute venue.

Returns:



15
16
17
# File 'lib/edoxen/venue_validator.rb', line 15

def venue
  @venue
end

Instance Method Details

#populate_from_registry!Object

Mutator: back-fill venue.city and venue.country_code from the UN/LOCODE entry. Raises KeyError when the unlocode is unknown or unset, so callers do not silently leave the venue untouched.

Returns the venue (so callers can chain).

Raises:

  • (KeyError)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/edoxen/venue_validator.rb', line 41

def populate_from_registry!
  raise KeyError, "populate_from_registry! requires kind: physical" unless venue.physical?
  raise KeyError, "populate_from_registry! requires unlocode" if venue.unlocode.nil? || venue.unlocode.to_s.empty?

  entry = Edoxen::ReferenceData.find_unlocode(venue.unlocode)
  raise KeyError, "Unknown UN/LOCODE: #{venue.unlocode}" if entry.nil?

  venue.city ||= entry.name
  venue.country_code ||= entry.country
  venue
end

#valid?Boolean

Pure boolean wrapper around validate.

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/edoxen/venue_validator.rb', line 31

def valid?
  validate
  errors.empty?
end

#validateArray[String]

Pure query. Populates errors and returns it; does not modify the venue.

Parameters:

  • auto_populate: (Boolean)

Returns:

  • (Array[String])


24
25
26
27
28
# File 'lib/edoxen/venue_validator.rb', line 24

def validate
  errors.clear
  validate_physical if venue.physical?
  errors
end