Class: TanukiEmoji::Db::UnicodeOrdering

Inherits:
Object
  • Object
show all
Defined in:
lib/tanuki_emoji/db/unicode_ordering.rb

Overview

Emoji Unicode Ordering database

Constant Summary collapse

DATA_FILE =
"#{::TanukiEmoji::Db::UNICODE_DATA_DIR}/emoji-ordering.txt".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, data_file: nil) ⇒ UnicodeOrdering

Returns a new instance of UnicodeOrdering.



17
18
19
20
# File 'lib/tanuki_emoji/db/unicode_ordering.rb', line 17

def initialize(index:, data_file: nil)
  @data_file = data_file || self.class.data_file
  @index = index
end

Instance Attribute Details

#data_fileObject (readonly)

Returns the value of attribute data_file.



15
16
17
# File 'lib/tanuki_emoji/db/unicode_ordering.rb', line 15

def data_file
  @data_file
end

Class Method Details

.data_fileObject



11
12
13
# File 'lib/tanuki_emoji/db/unicode_ordering.rb', line 11

def self.data_file
  File.expand_path(File.join(__dir__, '../../../', DATA_FILE))
end

Instance Method Details

#load!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tanuki_emoji/db/unicode_ordering.rb', line 22

def load!
  db = {}
  File.readlines(data_file, mode: 'r:UTF-8').each_with_index do |line, line_number|
    next if line.start_with?('#')

    tokens = line.split
    semicolon_offset = tokens.index(';')
    next if semicolon_offset.nil?

    codepoints_array = tokens[0...semicolon_offset].map do |token|
      token[2...token.length].hex
    end
    codepoints = codepoints_array.pack('U*')

    db[codepoints] = line_number
  end

  db.each do |codepoints, sort_key|
    emoji = @index.find_by_codepoints(codepoints)

    next unless emoji

    emoji.sort_key = sort_key
  end
end