class TanukiEmoji::Index

Index of known Emoji Characters

Attributes

all[R]

@return [Array<TanukiEmoji::Character>] a collection of TanukiEmoji::Character

Public Class Methods

new() click to toggle source
# File lib/tanuki_emoji/index.rb, line 98
def initialize
  @all = []

  load_data_files
end

Public Instance Methods

add(emoji) click to toggle source

Add a new Emoji to the index

@param [TanukiEmoji::Character] emoji

# File lib/tanuki_emoji/index.rb, line 17
def add(emoji)
  @name_index ||= {}
  @alpha_code_index ||= {}
  @codepoints_index ||= {}

  # Check if exists and index otherwise
  insertion_mutex.synchronize do
    raise ::TanukiEmoji::AlphaCodeAlreadyIndexedError.new emoji.name, emoji.alpha_code if @alpha_code_index.key? emoji.alpha_code
    raise ::TanukiEmoji::CodepointAlreadyIndexedError.new emoji.name, emoji.codepoints if @codepoints_index.key? emoji.codepoints

    emoji.codepoints_alternates.each do |codepoints|
      raise ::TanukiEmoji::CodepointAlreadyIndexedError.new emoji.name, codepoints if @codepoints_index.key? codepoints
    end

    @name_index[emoji.name] = emoji
    @alpha_code_index[emoji.alpha_code] = emoji
    @codepoints_index[emoji.codepoints] = emoji

    emoji.codepoints_alternates.each do |codepoints|
      @codepoints_index[codepoints] = emoji
    end

    emoji.aliases.each do |alpha_code|
      @name_index[::TanukiEmoji::Character.format_name(alpha_code)] = emoji
      @alpha_code_index[::TanukiEmoji::Character.format_alpha_code(alpha_code)] = emoji
    end
  end

  all << emoji

  emoji
end
alpha_code_pattern() click to toggle source

Return a regular expression that can be used to search for indexed `:alpha_codes:`

@return [Regexp] regular expression that matches indexed `:alpha_code:`

# File lib/tanuki_emoji/index.rb, line 83
def alpha_code_pattern
  /(?<=[^[:alnum:]:]|\n|^)
   :(#{@name_index.keys.map { |name| Regexp.escape(name) }.join('|')}):
   (?=[^[:alnum:]:]|$)/x
end
codepoints_pattern() click to toggle source

Return a regular expression that can be used to search for emoji codepoints

@return [Regexp] regular expression that matches indexed emoji codepoints

# File lib/tanuki_emoji/index.rb, line 92
def codepoints_pattern
  /(#{@codepoints_index.keys.map { |moji| Regexp.escape(moji) }.join('|')})/
end
find_by_alpha_code(alpha_code) click to toggle source

Find an Emoji by its :alpha_code:

@param [String] alpha_code @return [TanukiEmoji::Character]

# File lib/tanuki_emoji/index.rb, line 54
def find_by_alpha_code(alpha_code)
  @alpha_code_index[Character.format_alpha_code(alpha_code)]
end
find_by_codepoints(unicode_codepoints) click to toggle source

Find an Emoji by its Unicode representation

@param [String] unicode_codepoints @return [TanukiEmoji::Character]

# File lib/tanuki_emoji/index.rb, line 62
def find_by_codepoints(unicode_codepoints)
  @codepoints_index[unicode_codepoints]
end
reset!(reload: true) click to toggle source

Clears the index to start from scratch

@note This is intended to be used in test and development only @param [Boolean] reload whether to reload emoji database or leave it empty

# File lib/tanuki_emoji/index.rb, line 70
def reset!(reload: true)
  @all = []

  remove_instance_variable :@name_index if defined? @name_index
  remove_instance_variable :@alpha_code_index if defined? @alpha_code_index
  remove_instance_variable :@codepoints_index if defined? @codepoints_index

  load_data_files if reload
end

Private Instance Methods

insertion_mutex() click to toggle source
# File lib/tanuki_emoji/index.rb, line 104
def insertion_mutex
  @insertion_mutex ||= Mutex.new
end
load_data_files() click to toggle source
# File lib/tanuki_emoji/index.rb, line 108
def load_data_files
  Db::Gemojione.new(index: self).load!
  Db::UnicodeVersion.new(index: self).load!
end