Class: TanukiEmoji::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Singleton
Defined in:
lib/tanuki_emoji/index.rb

Overview

Index of known Emoji Characters

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allArray<TanukiEmoji::Character> (readonly)

Returns a collection of TanukiEmoji::Character.

Returns:



12
13
14
# File 'lib/tanuki_emoji/index.rb', line 12

def all
  @all
end

Instance Method Details

#add(emoji) ⇒ TanukiEmoji::Character

Add a new Emoji to the index

Parameters:

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tanuki_emoji/index.rb', line 18

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

    add_to_index(emoji)
  end

  all << emoji

  emoji
end

#alpha_code_patternRegexp

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

Returns:

  • (Regexp)

    regular expression that matches indexed ‘:alpha_code:`



83
84
85
86
87
# 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(exclude_text_presentation: false) ⇒ Regexp

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

Parameters:

  • exclude_text_presentation (Boolean) (defaults to: false)

    exclude codepoints and sequences with text presentation selector

Returns:

  • (Regexp)

    regular expression that matches indexed emoji codepoints



93
94
95
96
97
98
99
# File 'lib/tanuki_emoji/index.rb', line 93

def codepoints_pattern(exclude_text_presentation: false)
  possible_codepoints = sorted_codepoints.map { |moji, _| Regexp.escape(moji) }.join('|')
  variation_selector = ""
  variation_selector = /(?!#{TanukiEmoji::Character::PLAIN_VARIATION_SELECTOR_STRING})/o if exclude_text_presentation

  /(#{possible_codepoints})#{variation_selector}/
end

#find_by_alpha_code(alpha_code) ⇒ TanukiEmoji::Character

Find an Emoji by its :alpha_code:

Parameters:

  • alpha_code (String)

Returns:



50
51
52
53
54
# File 'lib/tanuki_emoji/index.rb', line 50

def find_by_alpha_code(alpha_code)
  return unless @alpha_code_index

  @alpha_code_index[Character.format_alpha_code(alpha_code)]
end

#find_by_codepoints(unicode_codepoints) ⇒ TanukiEmoji::Character

Find an Emoji by its Unicode representation

Parameters:

  • unicode_codepoints (String)

Returns:



60
61
62
63
64
# File 'lib/tanuki_emoji/index.rb', line 60

def find_by_codepoints(unicode_codepoints)
  return unless @codepoints_index

  @codepoints_index[unicode_codepoints]
end

#reset!(reload: true) ⇒ Object

Note:

This is intended to be used in test and development only

Clears the index to start from scratch

Parameters:

  • reload (Boolean) (defaults to: true)

    whether to reload emoji database or leave it empty



70
71
72
73
74
75
76
77
78
# 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

#update(emoji) ⇒ Object



40
41
42
43
44
# File 'lib/tanuki_emoji/index.rb', line 40

def update(emoji)
  insertion_mutex.synchronize do
    add_to_index(emoji)
  end
end