class GeezifyRb::Arabify
processing tools to convert Geeze numbers to arabic.
Constants
- ERROR_MSG1
- ERROR_MSG3
- ERROR_MSG_CONSTRUCTOR
- NUMHASH
Public Class Methods
arabify(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 23 def self.arabify(str) new(str).arabify end
new(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 17 def initialize(str) raise ArgumentError, ERROR_MSG_CONSTRUCTOR unless validinput?(str) @geezstr = str end
Public Instance Methods
arabify()
click to toggle source
# File lib/geezify-rb/arabify.rb, line 27 def arabify preprocessed = rollback(@geezstr.gsub('፼', '፼ ')).split('፼') preprocessed .each_with_index .reduce(0) do |sum, (v, i)| sum + convert_upto10000(v.strip) * (10_000**(preprocessed.length - 1 - i)) end end
Private Instance Methods
convert_2digit(string)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 46 def convert_2digit(string) str = string || '' raise ArgumentError, ERROR_MSG1 unless valid_for_2digit?(str) str.split('').sum { |x| x == '' ? 0 : NUMHASH[x] } end
convert_upto10000(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 53 def convert_upto10000(str) return unless valid_for_convupto10000?(str) pos100 = str.index('፻') return convert_2digit(str) if pos100.nil? return 100 + convert_2digit(str[1..-1]) if pos100.zero? convert_2digit(str[0..(pos100 - 1)]) * 100 + convert_2digit(str[(pos100 + 1)..-1]) end
rollback(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 40 def rollback(str) raise ArgumentError, ERROR_MSG3 unless validinput?(str) str.gsub('፼፻', '፼፩፻').gsub(/^፻/, '፩፻').gsub(/^፼/, '፩፼') end
valid_for_2digit?(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 72 def valid_for_2digit?(str) str.length <= 2 && str.split('') .map { |x| NUMHASH.key?(x) } .reduce(true) { |res, n| res && n } end
valid_for_convupto10000?(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 78 def valid_for_convupto10000?(str) str.is_a?(String) && str.length <= 5 && str.match('፼').nil? end
validinput?(str)
click to toggle source
# File lib/geezify-rb/arabify.rb, line 66 def validinput?(str) str.split('') .map { |x| NUMHASH.key?(x) || x == '፼' || x == '፻' } .reduce(true) { |result, n| result && n } end