Class: Phony::NationalSplitters::Variable
- Defined in:
- lib/phony/national_splitters/variable.rb
Instance Attribute Summary
Attributes inherited from Fixed
Instance Method Summary collapse
-
#initialize(fallback, ndcs, options = {}) ⇒ Variable
constructor
A new instance of Variable.
-
#length ⇒ Object
A valid length.
-
#split(national_number) ⇒ Object
Takes a national number and splits it into ndc and rest.
Methods inherited from Fixed
Methods inherited from DSL
Constructor Details
#initialize(fallback, ndcs, options = {}) ⇒ Variable
Returns a new instance of Variable.
9 10 11 12 |
# File 'lib/phony/national_splitters/variable.rb', line 9 def initialize fallback, ndcs, = {} super fallback, @ndcs = optimize ndcs end |
Instance Method Details
#length ⇒ Object
A valid length.
44 45 46 |
# File 'lib/phony/national_splitters/variable.rb', line 44 def length (@mapped_ndc_min_length..@mapped_ndc_max_length) end |
#split(national_number) ⇒ Object
Takes a national number and splits it into ndc and rest.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/phony/national_splitters/variable.rb', line 16 def split national_number fallback_number = national_number.dup # Extract a starting point. # # This if can possibly be removed. # presumed_code = if @mapped_ndc_min_length > 0 presumed_code = national_number.slice!(0..@mapped_ndc_min_length-1) else '' end # Try for all possible mapped. # @mapped_ndc_min_length.upto @mapped_ndc_max_length do |i| ndcs_of_size_i = @ndcs[i] return [@zero, presumed_code, national_number] unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code) presumed_code << national_number.slice!(0..0) end # Not found. # super fallback_number end |