Class: SecID::Detector Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sec_id/detector.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Detects which identifier types match a given string using a three-stage pipeline that eliminates most candidates before calling valid?.

Stage 1 — Special-character dispatch (O(1)):

Strings containing `/`, ` `, or `*@#` route to the only types accepting those chars.

Stage 2 — Length lookup (O(1) hash access):

Pre-computed table maps each possible length to candidate classes.

Stage 3 — Charset pre-filter:

Survivors are filtered by their VALID_CHARS_REGEX before calling `valid?`.

Typical result: 1-2 valid? calls instead of 14.

Instance Method Summary collapse

Constructor Details

#initialize(identifier_list) ⇒ Detector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Detector.

Parameters:

  • identifier_list (Array<Class>)

    registered identifier classes



21
22
23
24
# File 'lib/sec_id/detector.rb', line 21

def initialize(identifier_list)
  @classes = identifier_list.dup.freeze
  precompute
end

Instance Method Details

#call(str) ⇒ Array<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detects all matching identifier types for the given string.

Parameters:

  • str (String, nil)

    the identifier string to detect

Returns:

  • (Array<Symbol>)

    matching type symbols sorted by specificity



30
31
32
33
34
35
36
37
# File 'lib/sec_id/detector.rb', line 30

def call(str)
  input = str.to_s.strip
  return [] if input.empty?

  upcased = input.upcase
  candidates = filter_candidates(upcased)
  validate_and_sort(input, candidates)
end

#first_match(str) ⇒ SecID::Base?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the most-specific matching instance, built once and reused, or nil.

Parameters:

  • str (String, nil)

    the identifier string to parse

Returns:



55
56
57
58
59
60
61
62
# File 'lib/sec_id/detector.rb', line 55

def first_match(str)
  input = str.to_s.strip
  return if input.empty?

  candidates = filter_candidates(input.upcase)
  matches = candidates.filter_map { |klass| (i = klass.new(input)).valid? ? i : nil }
  matches.min_by { |instance| @priority_for[instance.class] }
end

#matches?(str) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether any registered type matches, short-circuiting on the first valid candidate without sorting or mapping to symbols.

Parameters:

  • str (String, nil)

    the identifier string to test

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/sec_id/detector.rb', line 44

def matches?(str)
  input = str.to_s.strip
  return false if input.empty?

  filter_candidates(input.upcase).any? { |klass| klass.valid?(input) }
end