Class: UkAccountValidator::ModulusWeightsTable

Inherits:
Object
  • Object
show all
Defined in:
lib/uk_account_validator/modulus_weights_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ModulusWeightsTable

Returns a new instance of ModulusWeightsTable.



4
5
6
7
8
9
10
11
12
# File 'lib/uk_account_validator/modulus_weights_table.rb', line 4

def initialize(path)
  @weights = []

  File.readlines(path).each do |line|
    @weights << ModulusWeight.from_line(line)
  end

  @weights.sort_by! { |weight| -weight.sort_code_start.to_i }
end

Instance Method Details

#find(sort_code) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uk_account_validator/modulus_weights_table.rb', line 14

def find(sort_code)
  sort_code = sort_code.to_i

  min_found_weight_index = @weights.bsearch_index do |w|
    w.sort_code_start.to_i <= sort_code
  end

  return [] if min_found_weight_index.nil?

  found_weights = []
  index = min_found_weight_index
  while index < @weights.size &&
          @weights[index].sort_code_start.to_i <= sort_code &&
          sort_code <= @weights[index].sort_code_end.to_i
    found_weights << @weights[index]
    index += 1
  end

  found_weights
end