Class: TwitterCldr::Collation::SortKeyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/collation/sort_key_builder.rb

Overview

SortKeyBuilder builds a collation sort key from an array of collation elements.

Weights compression algorithms for every level are described in source.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm

Constant Summary collapse

LEVEL_SEPARATOR =

separate levels in a sort key ‘01’ bytes

1
VALID_CASE_FIRST_OPTIONS =
[nil, :lower, :upper]
VALID_MAXIMUM_LEVEL_OPTIONS =
[nil, 1, 2, 3]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collation_elements, options = {}) ⇒ SortKeyBuilder

Arguments:

collation_elements - an array of collation elements, represented as arrays of integer weights.
options            - hash of options:
  case_first       - optional case-first sorting order setting: :upper, :lower, nil (discard case bits).
  maximum_level    - only append weights to maximum level specified (1 or 2), can be useful for searching/matching applications

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/twitter_cldr/collation/sort_key_builder.rb', line 48

def initialize(collation_elements, options = {})
  raise ArgumentError, "second argument should be an options hash, not `#{options}`. Do you mean `:case_first => #{options}`?" unless options.kind_of? Hash

  case_first = options[:case_first]
  raise ArgumentError, "invalid case-first options '#{case_first.inspect}'" unless VALID_CASE_FIRST_OPTIONS.include?(case_first)

  maximum_level = options[:maximum_level]
  raise ArgumentError, "invalid maximum_level option 'options[:maximum_level]'" unless VALID_MAXIMUM_LEVEL_OPTIONS.include?(maximum_level)

  @collation_elements = collation_elements
  @case_first         = case_first
  @maximum_level      = maximum_level

  init_tertiary_constants
end

Instance Attribute Details

#case_firstObject (readonly)

Returns the value of attribute case_first.



23
24
25
# File 'lib/twitter_cldr/collation/sort_key_builder.rb', line 23

def case_first
  @case_first
end

#collation_elementsObject (readonly)

Returns the value of attribute collation_elements.



23
24
25
# File 'lib/twitter_cldr/collation/sort_key_builder.rb', line 23

def collation_elements
  @collation_elements
end

Class Method Details

.build(collation_elements, options = nil) ⇒ Object

Returns a sort key as an array of bytes.

Arguments:

collation_elements - an array of collation elements, represented as arrays of integer weights.
options            - hash of options:
  case_first       - optional case-first sorting order setting: :upper, :lower, nil (discard case bits).
  maximum_level    - only append weights to maximum level specified (1 or 2), can be useful for searching/matching applications

An instance of the class is created only to prevent passing of @collation_elements and @bytes_array from one method into another while forming the sort key.



37
38
39
# File 'lib/twitter_cldr/collation/sort_key_builder.rb', line 37

def self.build(collation_elements, options = nil)
  new(collation_elements, options).bytes_array
end

Instance Method Details

#bytes_arrayObject



64
65
66
# File 'lib/twitter_cldr/collation/sort_key_builder.rb', line 64

def bytes_array
  @bytes_array ||= build_bytes_array
end