Module: Protokoll::ExtractNumber

Defined in:
lib/protokoll/extract_number.rb

Class Method Summary collapse

Class Method Details

.create_number_mask(format) ⇒ Object



3
4
5
6
7
# File 'lib/protokoll/extract_number.rb', line 3

def self.create_number_mask(format)
  format.split("").map do |i|
    (i == "#") ? 1 : 0
  end
end

.expand_pattern(pattern) ⇒ Object



21
22
23
# File 'lib/protokoll/extract_number.rb', line 21

def self.expand_pattern(pattern)
  pattern.sub("%Y", "0000")
end

.extract_using_mask(db_number, mask) ⇒ Object



9
10
11
12
13
14
# File 'lib/protokoll/extract_number.rb', line 9

def self.extract_using_mask(db_number, mask)
  result = db_number.split("").select.with_index do |n, i| 
    n if mask[i] == 1
  end
  result.join.to_i
end

.number(str, pattern) ⇒ Object



16
17
18
19
# File 'lib/protokoll/extract_number.rb', line 16

def self.number(str, pattern)
  mask = create_number_mask( expand_pattern(pattern) )
  extract_using_mask(str, mask)
end