Class: Vehicles::Plates::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/vehicles/plates.rb

Overview

The answer to "is this a plate, and which series issues it?".

Defined Under Namespace

Classes: Hit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, jurisdiction:, canon:, hits:) ⇒ Match

Returns a new instance of Match.



77
78
79
80
81
82
83
84
85
# File 'lib/vehicles/plates.rb', line 77

def initialize(input:, jurisdiction:, canon:, hits:)
  @input = input
  @jurisdiction = jurisdiction
  @canon = canon
  # Exact hits outrank lenient ones, strict-validated series outrank
  # recall-only catch-alls; series order (standard first) breaks ties.
  @hits = hits.sort_by { |h| [ h.exact? ? 0 : 1, h.series.strict? ? 0 : 1 ] }.freeze
  freeze
end

Instance Attribute Details

#canonObject (readonly)

Returns the value of attribute canon.



75
76
77
# File 'lib/vehicles/plates.rb', line 75

def canon
  @canon
end

#hitsObject (readonly)

Returns the value of attribute hits.



75
76
77
# File 'lib/vehicles/plates.rb', line 75

def hits
  @hits
end

#inputObject (readonly)

Returns the value of attribute input.



75
76
77
# File 'lib/vehicles/plates.rb', line 75

def input
  @input
end

#jurisdictionObject (readonly)

Returns the value of attribute jurisdiction.



75
76
77
# File 'lib/vehicles/plates.rb', line 75

def jurisdiction
  @jurisdiction
end

Instance Method Details

#bestObject



96
# File 'lib/vehicles/plates.rb', line 96

def best = hits.first&.series

#exact?Boolean

Returns:

  • (Boolean)


88
# File 'lib/vehicles/plates.rb', line 88

def exact? = hits.any?(&:exact?)

#formattedObject

The serial formatted the way the best-matching series prints it on the physical plate — "1234XYZ" typed, "1234 XYZ" issued.



100
101
102
# File 'lib/vehicles/plates.rb', line 100

def formatted
  best&.format_serial(canon)
end

#seriesObject



95
# File 'lib/vehicles/plates.rb', line 95

def series = hits.map(&:series)

#strict?Boolean

True when at least one hit is a strict-validated series — the strong signal ("the authority issues this alphabet"), vs a catch-all shape union like an export plate. Prefer this for user-input validation.

Returns:

  • (Boolean)


93
# File 'lib/vehicles/plates.rb', line 93

def strict? = hits.any? { |h| h.series.strict? }

#suggestionObject

Only when the user's punctuation was off: the "did you mean" string.



105
106
107
# File 'lib/vehicles/plates.rb', line 105

def suggestion
  formatted if valid? && !exact?
end

#valid?Boolean

Returns:

  • (Boolean)


87
# File 'lib/vehicles/plates.rb', line 87

def valid? = hits.any?