class TurkishRegexps::TrRegexp

Let your regexps to speak in Turkish

Attributes

casefold[R]
options[R]
source[R]

Public Class Methods

new(pattern) click to toggle source

A new instance of TrRegexp

@param [Regexp] pattern @return [TrRegexp]

# File lib/turkish_regexps/tr_regexp.rb, line 10
def initialize(pattern)
  @source = pattern.source
  @options = pattern.options
  @casefold = pattern.casefold?
end

Public Instance Methods

translate() click to toggle source

Translate regexps into Turkish supported version

@return [Regexp]

# File lib/turkish_regexps/tr_regexp.rb, line 19
def translate
  translate_matches
  add_meta_charset
  set_encoding
end

Private Instance Methods

add_meta_charset() click to toggle source

Expand meta characters to latin

@return [NilClass]

# File lib/turkish_regexps/tr_regexp.rb, line 60
def add_meta_charset
  meta = { '\w' => '[\p{Latin}\d_]', '\W' => '[^\p{Latin}\d_]' }.freeze
  meta.each { source.gsub!(_1, _2) }
  nil
end
complement_range(first, last) click to toggle source

Translate complement range of character class if regexp is case-insensitive

@param [String] first @param [String] last @return [String]

# File lib/turkish_regexps/tr_regexp.rb, line 53
def complement_range(first, last)
  casefold ? (TurkishRanges::TrText.new(first.swapcase)..TurkishRanges::TrText.new(last.swapcase)).to_a.join : ''
end
set_encoding() click to toggle source

Creates the last form of translated regexp, ready to use

@return [Regexp]

# File lib/turkish_regexps/tr_regexp.rb, line 69
def set_encoding
  Regexp.new(source.force_encoding('UTF-8'), Regexp::FIXEDENCODING | options)
end
translate_matches() click to toggle source

Find character class inside regexp, then change them with an extended version

@return [NilClass]

# File lib/turkish_regexps/tr_regexp.rb, line 32
def translate_matches
  char_class_re = /(.)-(.)/.freeze
  source.gsub!(char_class_re) { translate_range(*_1.split('-')) }
  nil
end
translate_range(first, last) click to toggle source

Translate range of character class

@param [String] first @param [String] last @return [String]

# File lib/turkish_regexps/tr_regexp.rb, line 43
def translate_range(first, last)
  (TurkishRanges::TrText.new(first)..TurkishRanges::TrText.new(last)).to_a.join \
    complement_range(first, last)
end