module GostTranslit

Main module

Constants

LATIN_MAPPING
LATIN_REPLACING_MAPPING
LOWER_REGEXP
RU_MAPPING
UPPER_REGEXP

Public Class Methods

convert(string) click to toggle source
# File lib/gost_translit.rb, line 92
def convert(string)
  language(string) == :rus ? to_latin(string) : to_cyrillic(string)
end
to_cyrillic(string) click to toggle source
# File lib/gost_translit.rb, line 77
def to_cyrillic(string)
  words = string.split(' ')

  words.map! do |word|
    translit_word = word.downcase
    LATIN_REPLACING_MAPPING.each { |k, v| translit_word.gsub!(k, v) }

    translit_word = translit_word.split('')
                                 .map! { |l| LATIN_MAPPING[l] || l }
                                 .join

    apply_capitalize_rules(word, translit_word)
  end.join(' ')
end
to_latin(string) click to toggle source
# File lib/gost_translit.rb, line 63
def to_latin(string)
  words = string.split(' ')

  words.map! do |word|
    translit_word = word.downcase
                        .split('')
                        .map { |l| RU_MAPPING[l.to_sym] || l }
                        .join

    translit_word.gsub!(/(cz)(?=[i|e|j|y])/, 'c')
    apply_capitalize_rules(word, translit_word)
  end.join(' ')
end

Private Class Methods

apply_capitalize_rules(word, translit_word) click to toggle source
# File lib/gost_translit.rb, line 102
def apply_capitalize_rules(word, translit_word)
  case
  when UPPER_REGEXP.match?(word[0]) && word.match?(LOWER_REGEXP)
    translit_word.capitalize
  when !word.match?(LOWER_REGEXP)
    translit_word.upcase
  else
    translit_word
  end
end
language(string) click to toggle source
# File lib/gost_translit.rb, line 98
def language(string)
  string.scan(/\w+/).empty? ? :rus : :eng
end