Module: TurkishSupportHelpers

Included in:
TurkishSupport
Defined in:
lib/turkish_support/helpers.rb,
lib/turkish_support/constants.rb

Constant Summary collapse

ALPHA =
{
  lower:    'abcçdefgğhıijklmnoöpqrsştuüvwxyz',
  upper:    'ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ',
  tr_lower: 'çğıiöşü',
  tr_upper: 'ÇĞIİÖŞÜ'
}
ALPHABET =
ALPHA[:upper] + ALPHA[:lower]
META_CHARS =
{
  '\w' => '[\p{Latin}\d_]',
  '\W' => '[^\p{Latin}\d_]'
}
RE_RE_METHS =

Regexp required methods

i(
  match
  scan
)
RE_OP_METHS =

Regexp optional methods

i(
  []
  []=
  =~
  index
  rindex
  partition
  rpartition
  slice
  slice!
  split
  sub
  sub!
  gsub
  gsub!
)
i(
  downcase
  downcase!
  upcase
  upcase!
  capitalize
  capitalize!
)
RANGE_REGEXP =
/\[[^\]]*?([#{ALPHABET}]-[#{ALPHABET}])[^\[]*?\]/
CONJUCTIONS =
%w(ve ile veya)
SPECIAL_CHARS =
%q{("'}

Instance Method Summary collapse

Instance Method Details

#conjuction?(string) ⇒ Boolean



46
47
48
# File 'lib/turkish_support/helpers.rb', line 46

def conjuction?(string)
  CONJUCTIONS.include? string
end

#prepare_for(meth, string) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/turkish_support/helpers.rb', line 25

def prepare_for(meth, string)
  valid_meths = i(upcase downcase capitalize)
  unless valid_meths.include?(meth) && string.is_a?(String)
    fail ArgumentError, 'Invalid arguments for method `prepare_for`!'
  end

  method("prepare_for_#{meth}").call(string)
end

#start_with_a_special_char?(string) ⇒ Boolean



50
51
52
# File 'lib/turkish_support/helpers.rb', line 50

def start_with_a_special_char?(string)
  string =~ /^[#{SPECIAL_CHARS}]/
end

#tr_char?(ch) ⇒ Boolean



34
35
36
# File 'lib/turkish_support/helpers.rb', line 34

def tr_char?(ch)
  tr_lower?(ch) || tr_upper?(ch)
end

#tr_lower?(ch) ⇒ Boolean



38
39
40
# File 'lib/turkish_support/helpers.rb', line 38

def tr_lower?(ch)
  ALPHA[:tr_lower].include? ch
end

#tr_upper?(ch) ⇒ Boolean



42
43
44
# File 'lib/turkish_support/helpers.rb', line 42

def tr_upper?(ch)
  ALPHA[:tr_upper].include? ch
end

#translate_range(range_as_string, casefold = false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/turkish_support/helpers.rb', line 16

def translate_range(range_as_string, casefold = false)
  return '' unless range_as_string

  range_as_string.gsub!(/\[\]/, '')
  first, last = range_as_string.split('-')

  expand_range(first, last, casefold)
end

#translate_regexp(pattern) ⇒ Object

rubocop:disable Metrics/AbcSize



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/turkish_support/helpers.rb', line 2

def translate_regexp(pattern) # rubocop:disable Metrics/AbcSize
  Regexp.new(pattern) unless pattern.is_a? Regexp
  re, options = pattern.source, pattern.options

  while re.match(RANGE_REGEXP)
    re.scan(RANGE_REGEXP).flatten.compact.each do |matching|
      re.gsub! matching, translate_range(matching, pattern.casefold?)
    end
  end

  META_CHARS.each { |k, v| re.gsub!(k, v) }
  Regexp.new(re.force_encoding('UTF-8'), Regexp::FIXEDENCODING | options)
end