Class: Slk::Services::EmojiSearcher
- Inherits:
-
Object
- Object
- Slk::Services::EmojiSearcher
- Defined in:
- lib/slk/services/emoji_searcher.rb
Overview
Searches standard and workspace custom emoji
Instance Method Summary collapse
-
#initialize(cache_dir:, emoji_dir:, on_debug: nil) ⇒ EmojiSearcher
constructor
A new instance of EmojiSearcher.
-
#search(query, workspaces: []) ⇒ Hash
Search emoji by pattern.
-
#search_standard(pattern) ⇒ Array<Hash>
Search standard emoji only.
-
#search_workspace(workspace, pattern) ⇒ Array<Hash>
Search a workspace’s custom emoji.
Constructor Details
#initialize(cache_dir:, emoji_dir:, on_debug: nil) ⇒ EmojiSearcher
Returns a new instance of EmojiSearcher.
7 8 9 10 11 |
# File 'lib/slk/services/emoji_searcher.rb', line 7 def initialize(cache_dir:, emoji_dir:, on_debug: nil) @cache_dir = cache_dir @emoji_dir = emoji_dir @on_debug = on_debug end |
Instance Method Details
#search(query, workspaces: []) ⇒ Hash
Search emoji by pattern
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/slk/services/emoji_searcher.rb', line 17 def search(query, workspaces: []) pattern = Regexp.new(Regexp.escape(query), Regexp::IGNORECASE) results = [] # Search standard emoji results.concat(search_standard(pattern)) # Search workspace custom emoji workspaces.each do |workspace| results.concat(search_workspace(workspace, pattern)) end # Group by source results.group_by { |r| r[:source] } end |
#search_standard(pattern) ⇒ Array<Hash>
Search standard emoji only
36 37 38 39 40 41 42 43 |
# File 'lib/slk/services/emoji_searcher.rb', line 36 def search_standard(pattern) gemoji = load_gemoji return [] unless gemoji gemoji.filter_map do |name, char| { name: name, char: char, source: 'standard' } if name.match?(pattern) end end |
#search_workspace(workspace, pattern) ⇒ Array<Hash>
Search a workspace’s custom emoji
49 50 51 52 53 54 55 56 57 |
# File 'lib/slk/services/emoji_searcher.rb', line 49 def search_workspace(workspace, pattern) workspace_dir = File.join(@emoji_dir, workspace.name) return [] unless Dir.exist?(workspace_dir) Dir.glob(File.join(workspace_dir, '*')).filter_map do |filepath| name = File.basename(filepath, '.*') { name: name, path: filepath, source: workspace.name } if name.match?(pattern) end end |