Module: Codecal::Mask

Included in:
Calc
Defined in:
lib/lib/mask.rb

Instance Method Summary collapse

Instance Method Details

#convert_masked_code_typo(masked_code) ⇒ Object

not used



51
52
53
# File 'lib/lib/mask.rb', line 51

def convert_masked_code_typo(masked_code)
  masked_code.gsub('1', 'l')
end

#get_mask_offset(mask) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lib/mask.rb', line 19

def get_mask_offset(mask)
  mask.split("").inject([]) do |offset,c|
    [[*'a'..'z'], [*'A'..'Z'], [*'0'..'9']].each do |m|
      if m.include?(c)
        offset.push(m.find_index(c))
        break
      end
    end
    offset
  end
end

#mask_alphabet_include?(code) ⇒ Boolean



31
32
33
34
35
36
# File 'lib/lib/mask.rb', line 31

def mask_alphabet_include?(code)
  code.split('').each do |c|
    return false unless @mask_alphabet.include?(c)
  end
  true
end

#mask_char(char, offset) ⇒ Object



38
39
40
41
42
# File 'lib/lib/mask.rb', line 38

def mask_char(char, offset)
  if all_digits?(char) && char.size == 1
    @mask_alphabet[(@mask_alphabet.find_index(@mask_alphabet[char.to_i]) + offset) % @mask_alphabet.size]
  end
end

#mask_code(offset, code) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/lib/mask.rb', line 3

def mask_code(offset, code)
  code_arr = code.size > 5 ? code.split("") : ("%06d" % code).split("")
  masked_code = code_arr.each_with_index.inject([]) do |codes, (c, i)|
    codes.push(mask_char(c, offset[ i % offset.size ]))
  end
  start_code = masked_code.pop
  masked_code.unshift(start_code).join
end

#un_mask_char(char, offset) ⇒ Object



44
45
46
47
48
# File 'lib/lib/mask.rb', line 44

def un_mask_char(char, offset)
  if mask_alphabet_include?(char)
    (@mask_alphabet.find_index(char) - offset) % @mask_alphabet.size
  end
end

#unmask_code(offset, masked_code) ⇒ Object



12
13
14
15
16
17
# File 'lib/lib/mask.rb', line 12

def unmask_code(offset, masked_code)
  masked_code = masked_code[1..-1] + masked_code[0]
  masked_code.downcase.split("").each_with_index.inject([]) do |code, (c, i)|
    code.push(un_mask_char(c, offset[ i % offset.size ]))
  end.join
end