Class: TwitterCldr::Collation::Collator
- Inherits:
-
Object
- Object
- TwitterCldr::Collation::Collator
- Defined in:
- lib/twitter_cldr/collation/collator.rb
Overview
Collator uses fractional collation elements table form CLDR to generate sort keys for Unicode strings as well as compare and sort such strings by generated sort keys.
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
Class Method Summary collapse
-
.default_trie ⇒ Object
Loads and memoizes the default fractional collation elements trie.
- .tailored_trie(locale) ⇒ Object
Instance Method Summary collapse
- #compare(string_a, string_b) ⇒ Object
- #get_collation_elements(string_or_code_points) ⇒ Object
-
#get_sort_key(string_or_code_points, method_options = {}) ⇒ Object
Second arg options, supports an option :maximum_level, to pass on to SortKeyBuilder :maximum_level.
-
#initialize(locale = nil) ⇒ Collator
constructor
A new instance of Collator.
- #sort(strings) ⇒ Object
- #sort!(strings) ⇒ Object
Constructor Details
#initialize(locale = nil) ⇒ Collator
Returns a new instance of Collator.
18 19 20 21 22 |
# File 'lib/twitter_cldr/collation/collator.rb', line 18 def initialize(locale = nil) @locale = TwitterCldr.convert_locale(locale) if locale @options = @trie = load_trie end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
16 17 18 |
# File 'lib/twitter_cldr/collation/collator.rb', line 16 def locale @locale end |
Class Method Details
.default_trie ⇒ Object
Loads and memoizes the default fractional collation elements trie.
154 155 156 |
# File 'lib/twitter_cldr/collation/collator.rb', line 154 def default_trie @default_trie ||= TwitterCldr::Collation::TrieLoader.load_default_trie.lock end |
.tailored_trie(locale) ⇒ Object
158 159 160 |
# File 'lib/twitter_cldr/collation/collator.rb', line 158 def tailored_trie(locale) tailored_tries_cache[locale] end |
Instance Method Details
#compare(string_a, string_b) ⇒ Object
33 34 35 |
# File 'lib/twitter_cldr/collation/collator.rb', line 33 def compare(string_a, string_b) string_a == string_b ? 0 : get_sort_key(string_a) <=> get_sort_key(string_b) end |
#get_collation_elements(string_or_code_points) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/twitter_cldr/collation/collator.rb', line 43 def get_collation_elements(string_or_code_points) code_points = get_normalized_code_points(string_or_code_points) result = [] result.concat(code_point_collation_elements(code_points)) until code_points.empty? result end |
#get_sort_key(string_or_code_points, method_options = {}) ⇒ Object
Second arg options, supports an option :maximum_level, to pass on to SortKeyBuilder :maximum_level.
39 40 41 |
# File 'lib/twitter_cldr/collation/collator.rb', line 39 def get_sort_key(string_or_code_points, = {}) TwitterCldr::Collation::SortKeyBuilder.build(get_collation_elements(string_or_code_points), case_first: @options[:case_first], maximum_level: [:maximum_level]) end |
#sort(strings) ⇒ Object
24 25 26 |
# File 'lib/twitter_cldr/collation/collator.rb', line 24 def sort(strings) strings.map{ |s| [s, get_sort_key(s)] }.sort{ |a, b| a[1] <=> b[1] }.map(&:first) end |
#sort!(strings) ⇒ Object
28 29 30 31 |
# File 'lib/twitter_cldr/collation/collator.rb', line 28 def sort!(strings) sort_keys = Hash.new { |hash, string| hash[string] = get_sort_key(string) } strings.replace(strings.sort_by { |s| sort_keys[s] }) end |