Module: RecordCache::Strategy::Util::Collator

Defined in:
lib/record_cache/strategy/util.rb

Overview

StringCollator uses the Rails transliterate method for collation

Class Method Summary collapse

Class Method Details

.clearObject



109
110
111
112
# File 'lib/record_cache/strategy/util.rb', line 109

def self.clear
  @collated.each { |string| string.send(:remove_instance_variable, :@rc_collated) }
  @collated.clear
end

.collate(string) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/record_cache/strategy/util.rb', line 114

def self.collate(string)
  collated = string.instance_variable_get(:@rc_collated)
  return collated if collated
  normalized = ActiveSupport::Multibyte::Unicode.normalize(ActiveSupport::Multibyte::Unicode.tidy_bytes(string || ''), :c).mb_chars
  collated = I18n.transliterate(normalized).downcase.mb_chars
  # transliterate will replace ignored/unknown chars with ? the following line replaces ? with the original character
  collated.chars.each_with_index{ |c, i| collated[i] = normalized[i] if c == '?' } if collated.index('?')
  # puts "collation: #{string} => #{collated.to_s}"
  string.instance_variable_set(:@rc_collated, collated)
  @collated << string
  collated
end