class SubCipher::SubCipherObject

The sub cipher class.

Public Class Methods

new(options) click to toggle source

The sub cipher contructor. @param options (see SubCipher.gen) @return (see SubCipher.gen) @raise (see SubCipher.gen)

# File lib/sub_cipher.rb, line 47
def initialize(options)
  opts = check_opt(options)
  opts.each do |key, value|
    if (!SUPPORTED_OPTS.key?(key) && !SUPPORTED_OPTS.values.map {|v| v[:abbr]}.include?(key))
      raise SubCipherError.new(:unknown_option, key)
    end
    case key
    when :seed
      @map = value.chars.to_a.uniq.shuffle
    when :map
      @map = value.chars.to_a.uniq
    when :keep_case
      @keep_case = (value ? true : false)
    end
  end
  support_keep_case = true
  @map.each do |m|
    if !ALPHABETS.include?(m.downcase)
      support_keep_case = false
      break
    end
  end
  @keep_case = (support_keep_case && @keep_case ? true : false)
  if @keep_case
    @map = @map.map{ |m| m.downcase }.uniq
  end
  seed = @map.sort
  @mapping = {}
  seed.each_with_index do |s, index|
    @mapping[s] = @map[index]
  end
end

Public Instance Methods

decode(str) click to toggle source

Decode the given string. @param str [String]

The encoded string to decode.

@return [String]

The decoded result.
# File lib/sub_cipher.rb, line 94
def decode(str)
  str.chars.map { |c| convert(c, true) }.join
end
encode(str) click to toggle source

Encode the given string. @param str [String]

The original string to encode.

@return [String]

The encoded result.
# File lib/sub_cipher.rb, line 85
def encode(str)
  str.chars.map { |c| convert(c) }.join
end
map() click to toggle source

Show the map string, for mapping display. For example, if the seed string is “abc” and map string is “cab”, then the mapping is “a” => “c”, “b” => “a”, “c” => “b”. @return [String]

The map string.
# File lib/sub_cipher.rb, line 103
def map
  @map.join
end
seed() click to toggle source

Show the seed string, for mapping display. For example, if the seed string is “abc” and map string is “cab”, then the mapping is “a” => “c”, “b” => “a”, “c” => “b”. @return [String]

The seed string.
# File lib/sub_cipher.rb, line 112
def seed
  @map.sort.join
end

Private Instance Methods

check_opt(opts) click to toggle source

Check the given options.

# File lib/sub_cipher.rb, line 148
def check_opt(opts)
  SUPPORTED_OPTS.each do |key, value|
    if (opts[key] != nil) && opts.keys.include?(value[:abbr])
      raise SubCipherError.new(:duplicated_option, opts)
    end
    opts.merge!(key => opts[value[:abbr]]) if opts[value[:abbr]] != nil
    if (opts[key] != nil) && (value[:type] != nil) && !(opts[key]).is_a?(value[:type])
      raise SubCipherError.new(:invalid_option_value, opts)
    end
  end
  DEFAULT_OPTS.merge(opts)
end
convert(char, reverse = false) click to toggle source

Covert single char with the mapping.

# File lib/sub_cipher.rb, line 119
def convert(char, reverse = false)
  if @keep_case
    if reverse
      if @mapping.value?(char)
        return @mapping.index(char)
      elsif @mapping.value?(char.downcase)
        return @mapping.index(char.downcase).upcase
      else
        return char
      end
    else
      if @mapping.key?(char)
        return @mapping[char]
      elsif @mapping.key?(char.downcase)
        return @mapping[char.downcase].upcase
      else
        return char
      end
    end
  else
    if reverse
      @mapping.value?(char) ? @mapping.index(char) : char
    else
      @mapping.key?(char) ? @mapping[char] : char
    end
  end
end