Class: TanukiEmoji::Db::AdditionalAliases

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

Overview

Reads and adds aliases from additional_aliases.json

Constant Summary collapse

DATA_FILE =
'vendor/additional_aliases.json'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, data_file: self.class.data_file) ⇒ AdditionalAliases

Returns a new instance of AdditionalAliases.



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

def initialize(index:, data_file: self.class.data_file)
  @data_file = 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/additional_aliases.rb', line 15

def data_file
  @data_file
end

Class Method Details

.data_fileObject



11
12
13
# File 'lib/tanuki_emoji/db/additional_aliases.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
# File 'lib/tanuki_emoji/db/additional_aliases.rb', line 22

def load!
  db = File.open(data_file, 'r:UTF-8') do |file|
    JSON.parse(file.read, symbolize_names: true)
  end

  db.each_value do |emoji_data|
    emoji = @index.find_by_codepoints(emoji_data[:emoji])

    next unless emoji

    emoji_data[:aliases].each do |code|
      emoji.add_alias(code)
    end

    emoji.noto_image = emoji_data[:noto_image] if emoji_data[:noto_image]

    @index.update(emoji)
  end
end