class Cryptogram::Mapper

Public Class Methods

new(alphabet:, shift:) click to toggle source
# File lib/cryptogram/mapper.rb, line 3
def initialize(alphabet:, shift:)
  @shift = shift

  @mapper = build_mapper(alphabet)
  @swapcased_mapper = build_mapper(alphabet.join.swapcase.chars)
end

Public Instance Methods

map_to_initial(char) click to toggle source
# File lib/cryptogram/mapper.rb, line 14
def map_to_initial(char)
  @mapper.key(char) || @swapcased_mapper.key(char) || char
end
map_to_shifted(char) click to toggle source
# File lib/cryptogram/mapper.rb, line 10
def map_to_shifted(char)
  @mapper.dig(char) || @swapcased_mapper.dig(char) || char
end

Private Instance Methods

build_mapper(alphabet) click to toggle source
# File lib/cryptogram/mapper.rb, line 20
def build_mapper(alphabet)
  shifted_alphabet = alphabet.rotate(@shift)

  Hash[alphabet.zip(shifted_alphabet)]
end