Module: RecordCache::Strategy::Base::Collator

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

Overview

StringCollator uses the Rails transliterate method for collation

Class Method Summary collapse

Class Method Details

.clearObject



134
135
136
137
# File 'lib/record_cache/strategy/base.rb', line 134

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

.collate(string) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/record_cache/strategy/base.rb', line 139

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