class StringDirection::CharactersStrategy

Strategy to detect direction from the scripts to which string characters belong

Constants

IGNORED_CHARS

Ignored characters: unicode marks, punctuations, symbols, separator and other general categories

Public Instance Methods

run(string) click to toggle source

Inspect to wich scripts characters belongs and infer from them the string direction. right-to-left scripts are those in {Configuration#rtl_scripts}

params [String] The string to inspect @return [String, nil]

# File lib/string-direction/strategies/characters_strategy.rb, line 11
def run(string)
  string = string.to_s
  if ltr_characters?(string) && rtl_characters?(string)
    bidi
  elsif ltr_characters?(string)
    ltr
  elsif rtl_characters?(string)
    rtl
  end
end

Private Instance Methods

ltr_characters?(string) click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 36
def ltr_characters?(string)
  string.match(ltr_regex)
end
ltr_regex() click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 28
def ltr_regex
  @ltr_regex ||= /[^#{rtl_script_character_classes}#{IGNORED_CHARS}]/
end
rtl_characters?(string) click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 32
def rtl_characters?(string)
  string.match(rtl_regex)
end
rtl_regex() click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 24
def rtl_regex
  @rtl_regex ||= /[#{rtl_script_character_classes}]/
end
rtl_script_character_classes() click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 40
def rtl_script_character_classes
  @rtl_script_character_classes ||= rtl_scripts.map { |script| "\\p{#{script}}" }.join
end
rtl_scripts() click to toggle source
# File lib/string-direction/strategies/characters_strategy.rb, line 44
def rtl_scripts
  StringDirection.configuration.rtl_scripts
end