Module: ArabicLetterConnector

Defined in:
lib/arabic-letter-connector/logic.rb,
lib/arabic-letter-connector/version.rb

Defined Under Namespace

Classes: CharacterInfo

Constant Summary collapse

VERSION =
"0.1.1"
@@charinfos =
nil

Class Method Summary collapse

Class Method Details

.determine_form(previous_char, next_char) ⇒ Object

Determine the form of the current character (:isolated, :initial, :medial, or :final), given the previous character and the next one. In Arabic, all characters can connect with a previous character, but not all letters can connect with the next character (this is determined by CharacterInfo#connects?).



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/arabic-letter-connector/logic.rb', line 31

def self.determine_form(previous_char, next_char)
  charinfos = self.charinfos
  if charinfos[previous_char] && charinfos[next_char]
    charinfos[previous_char].connects? ? :medial : :initial # If the current character does not connect, 
                                                            # its medial form will map to its final form,
                                                            # and its initial form will map to its isolated form.
  elsif charinfos[previous_char] # The next character is not an arabic character.
    charinfos[previous_char].connects? ? :final : :isolated 
  elsif charinfos[next_char] # The previous character is not an arabic character.
    :initial # If the current character does not connect, its initial form will map to its isolated form.
  else # Neither of the surrounding characters are arabic characters.
    :isolated
  end
end

.transform(str) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arabic-letter-connector/logic.rb', line 46

def self.transform(str)
  res = ""
  charinfos = self.charinfos
  previous_char = nil
  current_char = nil
  next_char = nil
  consume_character = lambda do |char|
    previous_char = current_char
    current_char = next_char
    next_char = char
    return unless current_char
    if charinfos.keys.include?(current_char)
      form = determine_form(previous_char, next_char)
      res += charinfos[current_char].formatted[form]
    else
      res += current_char
    end
  end
  str.each_char { |char| consume_character.call(char) }
  consume_character.call(nil)
  return res
end