class String

Mix-in necessary methods to make layout swap available

Constants

LANG
LAYOUTS
SPLIT_REGEX

Public Instance Methods

cyrillic?() click to toggle source

Detect whether the given string is cyrillic

# File lib/layout_convert.rb, line 21
def cyrillic?
  guess_layout.equal?(:cyr)
end
cyrillish?() click to toggle source

Detect whether the given string has more cyrillic letters

# File lib/layout_convert.rb, line 54
def cyrillish?
  !self.latinish?
end
latin?() click to toggle source

Detect whether the given string is latin

# File lib/layout_convert.rb, line 27
def latin?
  guess_layout.equal?(:lat)
end
latinish?() click to toggle source

Detect whether the given string has more latin letters

# File lib/layout_convert.rb, line 40
def latinish?
  latin_count = cyrillic_count = 0
  split_words.each do |word|
    if word.latin?
      latin_count+=1
    else
      cyrillic_count+=1
    end
  end
  latin_count > cyrillic_count
end
mixed?() click to toggle source

Detect whether the given string has both latin and cyrillic letters

# File lib/layout_convert.rb, line 34
def mixed?
  guess_layout.equal?(:mixed)
end
swap_layout() click to toggle source

Swap the layout

# File lib/layout_convert.rb, line 60
def swap_layout
  if self.latinish? || self.cyrillish?
    split_words.map do |word|
      layout_map = set_layout word
      word.scan(/./).map do |ch|
        layout_map[ch].nil? ? ch : layout_map[ch]
      end.join
    end.join ' '
  end
end

Private Instance Methods

guess_layout() click to toggle source

Guess the layout

# File lib/layout_convert.rb, line 76
def guess_layout
  letters = self.scan(/[[:alpha:]]/).uniq
  if (letters - LANG[:lat]).empty?
    :lat
  elsif (letters - LANG[:cyr]).empty?
    :cyr
  else
    :mixed
  end
end
set_layout(word = self) click to toggle source

Set the layout hash

# File lib/layout_convert.rb, line 97
def set_layout(word = self)
  if word.latin?
    Hash[LAYOUTS[:lat].zip LAYOUTS[:cyr]]
  elsif word.cyrillic?
    Hash[LAYOUTS[:cyr].zip LAYOUTS[:lat]]
  end
end
split_words() click to toggle source

Split words using tricky regex

# File lib/layout_convert.rb, line 90
def split_words
  self.scan SPLIT_REGEX
end