Class: TanukiEmoji::Index
- Inherits:
-
Object
- Object
- TanukiEmoji::Index
- Includes:
- Enumerable, Singleton
- Defined in:
- lib/tanuki_emoji/index.rb
Overview
Index of known Emoji Characters
Instance Attribute Summary collapse
-
#all ⇒ Array<TanukiEmoji::Character>
readonly
A collection of TanukiEmoji::Character.
Instance Method Summary collapse
-
#add(emoji) ⇒ TanukiEmoji::Character
Add a new Emoji to the index.
-
#alpha_code_pattern ⇒ Regexp
Return a regular expression that can be used to search for indexed ‘:alpha_codes:`.
-
#codepoints_pattern(exclude_text_presentation: false) ⇒ Regexp
Return a regular expression that can be used to search for emoji codepoints.
-
#find_by_alpha_code(alpha_code) ⇒ TanukiEmoji::Character
Find an Emoji by its :alpha_code:.
-
#find_by_codepoints(unicode_codepoints) ⇒ TanukiEmoji::Character
Find an Emoji by its Unicode representation.
-
#reset!(reload: true) ⇒ Object
Clears the index to start from scratch.
- #update(emoji) ⇒ Object
Instance Attribute Details
#all ⇒ Array<TanukiEmoji::Character> (readonly)
Returns a collection of TanukiEmoji::Character.
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
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_pattern ⇒ Regexp
Return a regular expression that can be used to search for indexed ‘:alpha_codes:`
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
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:
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
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
This is intended to be used in test and development only
Clears the index to start from scratch
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 |