i’m developing an application on Ruby on Rails and i’m stuck. I have a class that maps all the weird characters into a normal characters (i.e. é, è -> e ). I need this class in two models and since i don’t want to repeat code, i want to load it so the models can use it. Any idea how to do that?.
Edit *I upload the class*
class UtfAnalyzer < Ferret::Analysis::Analyzer
include Ferret::Analysis
CHARACTER_MAPPINGS = {
['à','á','â','ã','ä','å','ā','ă'] => 'a',
['æ'] => 'ae',
['ď','đ'] => 'd',
['ć','č','ĉ','ċ'] => 'c',
['è','é','ê','ë','ē','ę','ě','ĕ','ė',] => 'e',
['ƒ'] => 'f',
['ĝ','ğ','ġ','ģ'] => 'g',
['ĥ','ħ'] => 'h',
['ì','ì','í','î','ï','ī','ĩ','ĭ'] => 'i',
['į','ı','ij','ĵ'] => 'j',
['ķ','ĸ'] => 'k',
['ł','ľ','ĺ','ļ','ŀ'] => 'l',
['ñ','ń','ň','ņ','ʼn','ŋ'] => 'n',
['ò','ó','ô','õ','ö','ø','ō','ő','ŏ','ŏ'] => 'o',
['œ'] => 'oek',
['ą'] => 'q',
['ŕ','ř','ŗ'] => 'r',
['ś','š','ş','ŝ','ș'] => 's',
['ť','ţ','ŧ','ț'] => 't',
['ù','ú','û','ü','ū','ů','ű','ŭ','ũ','ų'] => 'u',
['ŵ'] => 'w',
['ý','ÿ','ŷ'] => 'y',
['ž','ż','ź'] => 'z'
}
HTML_MAPPINGS = {
['à', 'á', 'À', 'Á', 'ä', 'Ä'] => 'a',
['è', 'é', 'È', 'É', 'ë', 'Ë'] => 'e',
['ì', 'í', 'Ì', 'Í', 'ï', 'Ï'] => 'i',
['ò', 'ó', 'Ò', 'Ó', 'ö', 'Ö'] => 'o',
['ù', 'ú', 'Ù', 'Ú', 'ü', 'Ü'] => 'u',
['ç', 'Ç'] => 'ç'
}
def self.token_stream(field, str)
ts = MappingFilter.new(RegExpTokenizer.new(str, /([a-zA-Z0-9ÑñàáäèéëìíïòóöùúüÀÁÈÉËÌÍÏÒÓÖÙÚÜçÇ]|(&([aeiouAEIOU](acute|grave|uml)|ccedil|Ccedil);))+/), HTML_MAPPINGS)
ts = MappingFilter.new(StandardTokenizer.new(str), CHARACTER_MAPPINGS)
ts = MappingFilter.new(ts, CHARACTER_MAPPINGS)
ts = LowerCaseFilter.new(ts)
end
end
Use modules:
Then include the module in your models:
The method convert will be available for you in MyModel instance scope (as in
MyModel.new.convert). In order to make convert available for you in MyModel class scope (as inMyModel.convert) replaceinclude CharConverterwithextend CharConverter.You can find more info on the difference between extend and include here.