sec_id 7.1.1 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be4a59e18a04226f78492df3eb2ec1318af8e7c99044005bda0f7b9652ac7a33
4
- data.tar.gz: 6edd9226b4cc7b293a7fe1950f32aa0673ea0b44507b36ecc253da98f372d3c2
3
+ metadata.gz: 070e9688322c11aee220661a15bdc3a6feffeeda6a4d5859620e1e49ef948d0e
4
+ data.tar.gz: 218328185fb31b121fc9fa36cbb2f0d20d7f81542e41e0cab9a453b4e66f366a
5
5
  SHA512:
6
- metadata.gz: b4b95d04179e0aee0ff4c45dbc2e169a024d3933a2c8234de2319422d19773d34247d83b8f457317af93d536ac431406fca9455a454cc7159ae2a9bad8982955
7
- data.tar.gz: f479c160b3229763197b1ac1fe3b7d67f8a6fc3281d3607adf59fece50e89cc8dd2160eb2d4f94d32954d136bc2e8c8998b19bfcdf1e24f4e32a2a035f9e972a
6
+ metadata.gz: 027535125c743f70d1748956f39530df76f9c537de9b2547d800c55e34ded2d8125c5a3b067edf16016c1aa88eca73ea7cb9567372f1a1875868549969fb620f
7
+ data.tar.gz: 8f3345512540d6a5bfd2edbe436a44948e5c4a45a87a7637462467642b4fcacb590c4f37f675c71d8a38bf8f96f054af9191c2ff84c8798bc0bceff8b8d4a295
data/CHANGELOG.md CHANGED
@@ -8,6 +8,24 @@ and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [8.0.0] - 2026-10-30
12
+
13
+ ### Added
14
+
15
+ - Lefthook Git hooks for staged secret scanning, Ruby autocorrection, whitespace, commit messages, tests, types, and documentation coverage.
16
+ - CI checks library CRAP scores with crap4ruby and fresh test coverage, with a maximum score of 10.
17
+
18
+ ### Changed
19
+
20
+ - Reduce detector allocations by sorting candidate types once and stopping parsing at the first valid match. Unsupported input lengths no longer grow the detector cache.
21
+ - Reduce LEI and IBAN checksum allocations by reusing decimal expansions and preserving IBAN numeric characters.
22
+ - Reduce scanner allocations by sorting candidate types once, stopping at the first valid type, and reusing the validated instance.
23
+ - Reduce temporary allocations in ISIN, CUSIP, CEI, and FIGI checksum calculations by processing ASCII bytes directly.
24
+ - Refresh development dependencies, all four lockfiles, and GitHub Actions for frozen CI installs.
25
+
26
+ - **BREAKING:** Minimum Ruby version raised from 3.2 to 3.3 (Ruby 3.2 reached EOL on 2026-03-31)
27
+ - RBS: `SecID::CFI::AttributeSet#to_h` and `#as_json` are now typed `Hash[Symbol, Hash[Symbol, untyped]]` instead of `Hash[untyped, untyped]` (Steep 2.1 tuple inference)
28
+
11
29
  ## [7.1.1] - 2026-07-31
12
30
 
13
31
  Documentation and packaging only. No identifier, checksum, or public method changed.
@@ -285,7 +303,8 @@ Gem metadata only: corrected the gemspec description and README wording. No libr
285
303
 
286
304
  - ISIN numbers support: `SecID::ISIN`
287
305
 
288
- [Unreleased]: https://github.com/svyatov/sec_id/compare/v7.1.1...HEAD
306
+ [Unreleased]: https://github.com/svyatov/sec_id/compare/v8.0.0...HEAD
307
+ [8.0.0]: https://github.com/svyatov/sec_id/compare/v7.1.1...v8.0.0
289
308
  [7.1.1]: https://github.com/svyatov/sec_id/compare/v7.1.0...v7.1.1
290
309
  [7.1.0]: https://github.com/svyatov/sec_id/compare/v7.0.0...v7.1.0
291
310
  [7.0.0]: https://github.com/svyatov/sec_id/compare/v6.1.0...v7.0.0
data/README.md CHANGED
@@ -6,7 +6,7 @@ A Ruby toolkit for securities identifiers: validate, parse, normalize, detect, c
6
6
 
7
7
  - **Nothing to call, nothing to sign up for.** SecID validates all 16 identifier standards offline. No registry lookup, no API key, no network call anywhere in [`lib/`](lib/).
8
8
  - **9 of the 16 carry a checksum.** SecID validates it, restores a missing one, and repairs a wrong one.
9
- - **Zero runtime dependencies.** Runs on Ruby 3.2 or newer.
9
+ - **Zero runtime dependencies.** Runs on Ruby 3.3 or newer.
10
10
  - **Rails, if you want it.** An opt-in ActiveModel validator adds `validates :isin, sec_id: true`. It stays off the default require path, so the dependency count stays at zero.
11
11
  - **Typed.** Ships RBS signatures for the whole public API, checked by Steep in strict mode.
12
12
 
@@ -50,14 +50,14 @@ A Ruby toolkit for securities identifiers: validate, parse, normalize, detect, c
50
50
 
51
51
  ## Supported Ruby versions
52
52
 
53
- Ruby 3.2+ is required.
53
+ Ruby 3.3+ is required.
54
54
 
55
55
  ## Installation
56
56
 
57
57
  Add this line to your application's Gemfile:
58
58
 
59
59
  ```ruby
60
- gem 'sec_id', '~> 7.1'
60
+ gem 'sec_id', '~> 8.0'
61
61
  ```
62
62
 
63
63
  And then execute:
@@ -922,10 +922,26 @@ committed signatures fall out of sync with the tables.
922
922
 
923
923
  ## Development
924
924
 
925
- After checking out the repo, run `bin/setup` to install dependencies.
925
+ Install [Betterleaks](https://github.com/betterleaks/betterleaks) and make its `betterleaks` command available on your `PATH`.
926
+ After checking out the repo, run `bin/setup` to install dependencies and Git hooks.
926
927
  Then run `bundle exec rake` to run RuboCop, validate the RBS signatures, and run the specs.
927
928
  `bin/console` gives you an interactive prompt to experiment in.
928
929
 
930
+ Pre-commit scans staged changes for secrets and whitespace errors, and safely corrects staged Ruby files with RuboCop.
931
+ Corrected Ruby files are staged automatically.
932
+ Pre-push runs static type checks, type coverage, specs, runtime signature checks, and documentation coverage.
933
+ Commit messages must follow Conventional Commits.
934
+ Put personal hook overrides in the ignored `lefthook-local.yml` file.
935
+
936
+ Run all pre-push jobs manually with `bundle exec lefthook run pre-push`.
937
+
938
+ CI checks library methods with [crap4ruby](https://github.com/svyatov/crap4ruby), using fresh test coverage and a maximum CRAP score of 10.
939
+ Run the same check locally:
940
+
941
+ ```sh
942
+ COVERAGE=1 bundle exec rake spec crap
943
+ ```
944
+
929
945
  To use your working copy from another project, point its Gemfile at the checkout:
930
946
  `gem 'sec_id', path: '/path/to/sec_id'`.
931
947
 
@@ -941,6 +957,12 @@ One person actively maintains SecID. They read bug reports and pull requests, an
941
957
 
942
958
  Bug reports and pull requests are welcome on [GitHub](https://github.com/svyatov/sec_id). See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, code style, and PR guidelines.
943
959
 
960
+ For checksum throughput and allocation comparisons, run `ruby -Ilib benchmark/checksums.rb`. It covers Luhn checksums, LEI, and IBAN, including numeric and letter-heavy inputs. It prints repeated samples as JSON with garbage collection enabled. Run the same script with `-I/path/to/baseline/lib` to compare another checkout on the same Ruby version.
961
+
962
+ For scanner comparisons, run `ruby -Ilib benchmark/scanner.rb`. It measures paragraphs, dense identifiers, noise, type filters, ambiguous identifiers, and compound patterns. JSON output includes matches to check correctness alongside timings and allocations. Use the same baseline option to compare another checkout.
963
+
964
+ For detection and parsing comparisons, run `ruby -Ilib benchmark/detector.rb`. It measures valid identifiers, ambiguous inputs, invalid checksums, and unsupported lengths. JSON output includes results, timings, and allocations with garbage collection enabled. Use the same baseline option to compare another checkout.
965
+
944
966
  This project follows the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).
945
967
 
946
968
  ## Changelog
@@ -10,7 +10,7 @@ module SecID
10
10
  #
11
11
  # @api private
12
12
  # @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
13
- # rubocop:disable Metrics/CollectionLiteralLength
13
+ # rubocop:disable-next Metrics/CollectionLiteralLength
14
14
  COUNTRY_CODES = Set.new(
15
15
  %w[
16
16
  AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ
@@ -41,6 +41,5 @@ module SecID
41
41
  XK
42
42
  ]
43
43
  ).freeze
44
- # rubocop:enable Metrics/CollectionLiteralLength
45
44
  end
46
45
  end
data/lib/sec_id/cei.rb CHANGED
@@ -55,7 +55,7 @@ module SecID
55
55
  # @raise [InvalidFormatError] if the CEI format is invalid
56
56
  def calculate_checksum
57
57
  validate_format_for_calculation!
58
- mod10(luhn_sum_double_add_double(reversed_digits_single(identifier)))
58
+ mod10(luhn_sum_single(identifier))
59
59
  end
60
60
 
61
61
  # Generates a random CEI body: 1 letter + 1 digit + 7 alphanumeric characters.
@@ -36,8 +36,11 @@ module SecID
36
36
  end
37
37
 
38
38
  # @return [Hash{Symbol => Hash}]
39
+ # @raise [ArgumentError] if a field has no meaning (category/group fields; never the case for attributes)
39
40
  def to_h
40
- @fields.to_h { |field| [field.meaning, field.to_h] }
41
+ @fields.to_h do |field|
42
+ [field.meaning || raise(ArgumentError, "attribute field #{field.code} has no meaning"), field.to_h]
43
+ end
41
44
  end
42
45
 
43
46
  # @return [Hash{Symbol => Hash}]
@@ -21,7 +21,7 @@ module SecID
21
21
  # +:not_applicable+, so it is never listed in a value map.
22
22
  #
23
23
  # @api private
24
- # rubocop:disable Metrics/ModuleLength
24
+ # rubocop:disable-next Metrics/ModuleLength
25
25
  module Tables
26
26
  # Marker for a pure not-applicable attribute position (accepts only +X+).
27
27
  NA = nil
@@ -1028,6 +1028,5 @@ module SecID
1028
1028
  GROUPS.dig(category_code, group_code)
1029
1029
  end
1030
1030
  end
1031
- # rubocop:enable Metrics/ModuleLength
1032
1031
  end
1033
1032
  end
@@ -22,26 +22,12 @@ module SecID
22
22
  #
23
23
  # def calculate_checksum
24
24
  # validate_format_for_calculation!
25
- # mod10(luhn_sum_standard(reversed_digits_multi(identifier)))
25
+ # mod10(luhn_sum_standard(identifier))
26
26
  # end
27
27
  # end
28
28
  #
29
29
  # @see https://en.wikipedia.org/wiki/Luhn_algorithm
30
30
  module Checkable
31
- # Character-to-digit mapping for Luhn algorithm variants.
32
- # Maps alphanumeric characters to digit arrays for multi-digit expansion.
33
- # Used by ISIN for checksum calculation.
34
- CHAR_TO_DIGITS = {
35
- '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
36
- '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
37
- 'A' => [1, 0], 'B' => [1, 1], 'C' => [1, 2], 'D' => [1, 3], 'E' => [1, 4],
38
- 'F' => [1, 5], 'G' => [1, 6], 'H' => [1, 7], 'I' => [1, 8], 'J' => [1, 9],
39
- 'K' => [2, 0], 'L' => [2, 1], 'M' => [2, 2], 'N' => [2, 3], 'O' => [2, 4],
40
- 'P' => [2, 5], 'Q' => [2, 6], 'R' => [2, 7], 'S' => [2, 8], 'T' => [2, 9],
41
- 'U' => [3, 0], 'V' => [3, 1], 'W' => [3, 2], 'X' => [3, 3], 'Y' => [3, 4], 'Z' => [3, 5],
42
- '*' => [3, 6], '@' => [3, 7], '#' => [3, 8]
43
- }.freeze
44
-
45
31
  # Character-to-digit mapping for single-digit conversion.
46
32
  # Maps alphanumeric characters to values 0-38 (A=10, B=11, ..., Z=35, *=36, @=37, #=38).
47
33
  # Used by CUSIP, FIGI, SEDOL, LEI, and IBAN for checksum calculations.
@@ -56,6 +42,15 @@ module SecID
56
42
  '*' => 36, '@' => 37, '#' => 38
57
43
  }.freeze
58
44
 
45
+ # ASCII byte lookup avoids allocating a String for each checksum character.
46
+ BYTE_TO_DIGIT = CHAR_TO_DIGIT.transform_keys(&:ord).freeze
47
+
48
+ # Decimal expansions reused by LEI and IBAN without allocating replacement strings.
49
+ DECIMAL_VALUES = CHAR_TO_DIGIT.transform_values { |value| value.to_s.freeze }.freeze
50
+
51
+ # Doubled decimal digits with their digits summed, for expanded ISIN Luhn.
52
+ LUHN_DOUBLE = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9].freeze
53
+
59
54
  # Extends the including identifier class with the concern's class methods and checksum reader.
60
55
  #
61
56
  # @param base [Class] the identifier class including this concern
@@ -162,61 +157,46 @@ module SecID
162
157
 
163
158
  private
164
159
 
165
- # CUSIP/CEI style: "Double Add Double" algorithm.
166
- # Processes pairs of digits, doubling the first (even-positioned from right),
167
- # then summing both digit's div10mod10 values.
168
- #
169
- # @param digits [Array<Integer>] reversed array of digit values
170
- # @return [Integer] the Luhn sum
171
- def luhn_sum_double_add_double(digits)
172
- digits.each_slice(2).reduce(0) do |sum, (even, odd)|
173
- double_even = (even || 0) * 2
174
- sum + div10mod10(double_even) + div10mod10(odd || 0)
175
- end
176
- end
177
-
178
- # FIGI style: index-based doubling algorithm.
179
- # Doubles odd-indexed digits (from right), then sums div10mod10 values.
160
+ # CUSIP/CEI double the rightmost body character; FIGI starts one position left.
161
+ # Input must already have passed the identifier's ASCII format validation.
180
162
  #
181
- # @param digits [Array<Integer>] reversed array of digit values
163
+ # @param id [String] validated identifier body
164
+ # @param double_first [Boolean] whether to double the rightmost value
182
165
  # @return [Integer] the Luhn sum
183
- def luhn_sum_indexed(digits)
184
- digits.each_with_index.reduce(0) do |sum, (digit, index)|
185
- digit *= 2 if index.odd?
186
- sum + div10mod10(digit)
166
+ def luhn_sum_single(id, double_first: true)
167
+ # @type var id: String
168
+ sum = 0
169
+ double = double_first
170
+ (id.bytesize - 1).downto(0) do |index|
171
+ value = BYTE_TO_DIGIT.fetch(id.getbyte(index))
172
+ value *= 2 if double
173
+ sum += div10mod10(value)
174
+ double = !double
187
175
  end
176
+ sum
188
177
  end
189
178
 
190
- # ISIN style: standard Luhn with subtract-9 for values > 9.
191
- # Processes pairs of digits, doubling the first (even-positioned from right),
192
- # subtracting 9 if result > 9.
179
+ # ISIN expands letters into two decimal digits before applying Luhn.
180
+ # A letter consumes two positions, so only a numeric character flips parity.
193
181
  #
194
- # @param digits [Array<Integer>] reversed array of digit values
182
+ # @param id [String] validated identifier body
195
183
  # @return [Integer] the Luhn sum
196
- def luhn_sum_standard(digits)
197
- digits.each_slice(2).reduce(0) do |sum, (even, odd)|
198
- double_even = (even || 0) * 2
199
- double_even -= 9 if double_even > 9
200
- sum + double_even + (odd || 0)
184
+ def luhn_sum_standard(id)
185
+ # @type var id: String
186
+ sum = 0
187
+ double = true
188
+ (id.bytesize - 1).downto(0) do |index|
189
+ value = BYTE_TO_DIGIT.fetch(id.getbyte(index))
190
+ units = value % 10
191
+ sum += double ? LUHN_DOUBLE.fetch(units) : units
192
+ if value >= 10
193
+ tens = value / 10
194
+ sum += double ? tens : LUHN_DOUBLE.fetch(tens)
195
+ else
196
+ double = !double
197
+ end
201
198
  end
202
- end
203
-
204
- # Converts identifier characters to reversed digit array using single-digit mapping.
205
- # Used by CUSIP, CEI, FIGI, and SEDOL.
206
- #
207
- # @param id [String] the identifier string
208
- # @return [Array<Integer>] reversed array of digit values
209
- def reversed_digits_single(id)
210
- id.each_char.map { |c| CHAR_TO_DIGIT.fetch(c) }.reverse!
211
- end
212
-
213
- # Converts identifier characters to reversed digit array using multi-digit mapping.
214
- # Used by ISIN where letters expand to two digits.
215
- #
216
- # @param id [String] the identifier string
217
- # @return [Array<Integer>] reversed array of digit values
218
- def reversed_digits_multi(id)
219
- id.each_char.flat_map { |c| CHAR_TO_DIGITS.fetch(c) }.reverse!
199
+ sum
220
200
  end
221
201
 
222
202
  # Returns error codes including checksum validation.
data/lib/sec_id/cusip.rb CHANGED
@@ -61,7 +61,7 @@ module SecID
61
61
  # @raise [InvalidFormatError] if the CUSIP format is invalid
62
62
  def calculate_checksum
63
63
  validate_format_for_calculation!
64
- mod10(luhn_sum_double_add_double(reversed_digits_single(identifier)))
64
+ mod10(luhn_sum_single(identifier))
65
65
  end
66
66
 
67
67
  # Generates a random CUSIP body: 8 alphanumeric characters.
@@ -19,7 +19,7 @@ module SecID
19
19
  class Detector
20
20
  # @param identifier_list [Array<Class>] registered identifier classes
21
21
  def initialize(identifier_list)
22
- @classes = identifier_list.dup.freeze
22
+ @classes = identifier_list.sort_by(&:detection_priority).freeze
23
23
  precompute
24
24
  end
25
25
 
@@ -33,7 +33,7 @@ module SecID
33
33
 
34
34
  upcased = input.upcase
35
35
  candidates = filter_candidates(upcased)
36
- validate_and_sort(input, candidates)
36
+ valid_types(input, candidates)
37
37
  end
38
38
 
39
39
  # Returns whether any registered type matches, short-circuiting on the first
@@ -57,8 +57,11 @@ module SecID
57
57
  return if input.empty?
58
58
 
59
59
  candidates = filter_candidates(input.upcase)
60
- matches = candidates.filter_map { |klass| (i = klass.new(input)).valid? ? i : nil }
61
- matches.min_by { |instance| instance.class.detection_priority }
60
+ candidates.each do |klass|
61
+ instance = klass.new(input)
62
+ return instance if instance.valid?
63
+ end
64
+ nil
62
65
  end
63
66
 
64
67
  private
@@ -74,14 +77,13 @@ module SecID
74
77
  stage3_charset(upcased, candidates)
75
78
  end
76
79
 
77
- # Validates candidates and returns sorted symbol keys.
80
+ # Validates candidates in precomputed specificity order.
78
81
  #
79
82
  # @param input [String]
80
83
  # @param candidates [Array<Class>]
81
84
  # @return [Array<Symbol>]
82
- def validate_and_sort(input, candidates)
85
+ def valid_types(input, candidates)
83
86
  matches = candidates.select { |klass| klass.valid?(input) }
84
- matches.sort_by!(&:detection_priority)
85
87
  matches.map!(&:type_key)
86
88
  end
87
89
 
@@ -105,11 +107,12 @@ module SecID
105
107
  #
106
108
  # @return [void]
107
109
  def build_length_table
108
- @candidates_by_length = Hash.new { |h, k| h[k] = [] }
110
+ @candidates_by_length = {}
109
111
  @classes.each do |klass|
110
- klass.length_values.each { |len| @candidates_by_length[len] << klass }
112
+ klass.length_values.each { |len| (@candidates_by_length[len] ||= []) << klass }
111
113
  end
112
114
  @candidates_by_length.each_value(&:freeze)
115
+ @candidates_by_length.default = [].freeze
113
116
  end
114
117
 
115
118
  # Stage 1: route strings with special characters to the only types that accept them.
@@ -130,7 +133,7 @@ module SecID
130
133
  # @param length [Integer]
131
134
  # @return [Array<Class>]
132
135
  def stage2_length(length)
133
- @candidates_by_length[length] || []
136
+ @candidates_by_length[length]
134
137
  end
135
138
 
136
139
  # Stage 3: filter candidates by character set.
data/lib/sec_id/figi.rb CHANGED
@@ -69,7 +69,7 @@ module SecID
69
69
  # @raise [InvalidFormatError] if the FIGI format is invalid
70
70
  def calculate_checksum
71
71
  validate_format_for_calculation!
72
- mod10(luhn_sum_indexed(reversed_digits_single(identifier)))
72
+ mod10(luhn_sum_single(identifier, double_first: false))
73
73
  end
74
74
 
75
75
  # Generates a random FIGI body: non-reserved 2-char prefix + 'G' + 8-char random part.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SecID
4
- # rubocop:disable Metrics/ClassLength
4
+ # rubocop:disable-next Metrics/ClassLength
5
5
  class IBAN < Base
6
6
  # Country-specific BBAN rules for EU/EEA countries
7
7
  # Each entry defines:
@@ -265,5 +265,4 @@ module SecID
265
265
  'XK' => 16 # Kosovo
266
266
  }.freeze
267
267
  end
268
- # rubocop:enable Metrics/ClassLength
269
268
  end
data/lib/sec_id/iban.rb CHANGED
@@ -226,7 +226,7 @@ module SecID
226
226
 
227
227
  # @return [String] the numeric string representation
228
228
  def numeric_string_for_check
229
- "#{bban}#{country_code}00".each_char.map { |char| CHAR_TO_DIGIT.fetch(char) }.join
229
+ "#{bban}#{country_code}00".gsub(/[^0-9]/) { |char| DECIMAL_VALUES.fetch(char) }
230
230
  end
231
231
  end
232
232
  end
data/lib/sec_id/isin.rb CHANGED
@@ -86,7 +86,7 @@ module SecID
86
86
  # @raise [InvalidFormatError] if the ISIN format is invalid
87
87
  def calculate_checksum
88
88
  validate_format_for_calculation!
89
- mod10(luhn_sum_standard(reversed_digits_multi(identifier)))
89
+ mod10(luhn_sum_standard(identifier))
90
90
  end
91
91
 
92
92
  # Generates a random ISIN body: 2-letter country code + 9-character NSIN.
data/lib/sec_id/lei.rb CHANGED
@@ -90,7 +90,7 @@ module SecID
90
90
 
91
91
  # @return [String] the numeric string representation
92
92
  def numeric_identifier
93
- identifier.each_char.map { |char| CHAR_TO_DIGIT.fetch(char) }.join
93
+ identifier.each_char.with_object(+'') { |char, numeric| numeric << DECIMAL_VALUES.fetch(char) }
94
94
  end
95
95
  end
96
96
  end
@@ -73,7 +73,7 @@ module SecID
73
73
  @classes.each do |klass|
74
74
  klass.length_values.each { |len| @candidates_by_length[len] << klass }
75
75
  end
76
- @candidates_by_length.each_value(&:freeze)
76
+ @candidates_by_length.each_value { |classes| classes.sort_by!(&:detection_priority).freeze }
77
77
  end
78
78
 
79
79
  # @param input [String]
@@ -118,18 +118,23 @@ module SecID
118
118
  return unless best
119
119
 
120
120
  end_pos = start_pos + raw.length
121
- Match.new(type: best.type_key, raw: raw, range: start_pos...end_pos, identifier: best.new(cleaned))
121
+ Match.new(type: best.class.type_key, raw: raw, range: start_pos...end_pos, identifier: best)
122
122
  end
123
123
 
124
- # @return [Class, nil]
124
+ # @return [Base, nil]
125
125
  def best_match(cleaned, classes)
126
126
  return if classes.empty?
127
127
 
128
- candidates = (@candidates_by_length[cleaned.length] || []) & classes
129
- return if candidates.empty?
128
+ candidates = @candidates_by_length[cleaned.length]
129
+ return unless candidates
130
130
 
131
- validated = candidates.select { |k| cleaned.match?(k::VALID_CHARS_REGEX) && k.valid?(cleaned) }
132
- validated.min_by(&:detection_priority)
131
+ candidates.each do |klass|
132
+ next unless classes.include?(klass) && cleaned.match?(klass::VALID_CHARS_REGEX)
133
+
134
+ instance = klass.new(cleaned)
135
+ return instance if instance.valid?
136
+ end
137
+ nil
133
138
  end
134
139
  end
135
140
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SecID
4
4
  # Current gem version.
5
- VERSION = '7.1.1'
5
+ VERSION = '8.0.0'
6
6
  end
data/sec_id.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.homepage = 'https://github.com/svyatov/sec_id'
23
23
  spec.license = 'MIT'
24
24
 
25
- spec.required_ruby_version = '>= 3.2.0'
25
+ spec.required_ruby_version = '>= 3.3.0'
26
26
 
27
27
  spec.require_paths = ['lib']
28
28
  spec.files = Dir['lib/**/*.rb'] + Dir['sig/**/*'] +
@@ -12,8 +12,8 @@ module SecID
12
12
  | () -> Enumerator[Field, self]
13
13
  def []: (Symbol meaning) -> Field?
14
14
  def empty?: () -> bool
15
- def to_h: () -> Hash[untyped, untyped]
16
- def as_json: (*untyped) -> Hash[Symbol, untyped]
15
+ def to_h: () -> Hash[Symbol, Hash[Symbol, untyped]]
16
+ def as_json: (*untyped) -> Hash[Symbol, Hash[Symbol, untyped]]
17
17
 
18
18
  # Per-meaning readers — the union of every attribute meaning. Each instance
19
19
  # only answers the meanings actually present; an absent one raises
@@ -1,8 +1,10 @@
1
1
  module SecID
2
2
  # Provides checksum validation and calculation for identifiers with a checksum.
3
3
  module Checkable : SecID::Base
4
- CHAR_TO_DIGITS: Hash[String, Integer | Array[Integer]]
5
4
  CHAR_TO_DIGIT: Hash[String, Integer]
5
+ BYTE_TO_DIGIT: Hash[Integer, Integer]
6
+ DECIMAL_VALUES: Hash[String, String]
7
+ LUHN_DOUBLE: Array[Integer]
6
8
 
7
9
  def self.included: (Module base) -> void
8
10
 
@@ -33,11 +35,10 @@ module SecID
33
35
 
34
36
  private
35
37
 
36
- def luhn_sum_double_add_double: (Array[Integer] digits) -> Integer
37
- def luhn_sum_indexed: (Array[Integer] digits) -> Integer
38
- def luhn_sum_standard: (Array[Integer] digits) -> Integer
39
- def reversed_digits_single: (untyped id) -> Array[Integer]
40
- def reversed_digits_multi: (untyped id) -> Array[Integer]
38
+ # Readers are nilable before validation. The callers validate first; local
39
+ # annotations keep the byte traversal typed without adding runtime guards.
40
+ def luhn_sum_single: (untyped id, ?double_first: bool) -> Integer
41
+ def luhn_sum_standard: (untyped id) -> Integer
41
42
  def error_codes: () -> Array[Symbol]
42
43
  def checksum_width: () -> Integer
43
44
  def validation_message: (Symbol code) -> String?
@@ -11,7 +11,7 @@ module SecID
11
11
  private
12
12
 
13
13
  def filter_candidates: (String upcased) -> Array[singleton(Base)]
14
- def validate_and_sort: (String input, Array[untyped] candidates) -> Array[Symbol]
14
+ def valid_types: (String input, Array[untyped] candidates) -> Array[Symbol]
15
15
  def precompute: () -> void
16
16
  def build_discriminator_sets: () -> void
17
17
  def build_length_table: () -> void
@@ -33,7 +33,7 @@ module SecID
33
33
  def scan_text: (String input, Array[singleton(Base)] target_classes) { (Match) -> void } -> void
34
34
  def identify_candidate: (untyped match_data, Array[singleton(Base)] target_classes) -> Match?
35
35
  def try_classes: (String raw, String cleaned, Integer start_pos, Array[singleton(Base)] classes) -> Match?
36
- def best_match: (String cleaned, Array[singleton(Base)] classes) -> singleton(Base)?
36
+ def best_match: (String cleaned, Array[singleton(Base)] classes) -> Base?
37
37
 
38
38
  @classes: Array[singleton(Base)]
39
39
  @fisn_classes: Array[singleton(Base)]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sec_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.1
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonid Svyatov
@@ -119,14 +119,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
119
  requirements:
120
120
  - - ">="
121
121
  - !ruby/object:Gem::Version
122
- version: 3.2.0
122
+ version: 3.3.0
123
123
  required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  requirements:
125
125
  - - ">="
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
128
  requirements: []
129
- rubygems_version: 4.0.16
129
+ rubygems_version: 4.0.20
130
130
  specification_version: 4
131
131
  summary: 'A Ruby toolkit for securities identifiers: validate, parse, normalize, detect,
132
132
  convert, generate, classify, and repair.'