Module: Legion::Tools::TriggerIndex
- Defined in:
- lib/legion/tools/trigger_index.rb
Class Method Summary collapse
- .build_async! ⇒ Object
- .build_from_registry ⇒ Object
- .clear ⇒ Object
- .empty? ⇒ Boolean
- .match(word_set) ⇒ Object
- .size ⇒ Object
Class Method Details
.build_async! ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/legion/tools/trigger_index.rb', line 25 def build_async! if defined?(Concurrent::Promises) Concurrent::Promises.future { build_from_registry } else build_from_registry end end |
.build_from_registry ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/legion/tools/trigger_index.rb', line 14 def build_from_registry clear Registry.all_tools.each do |tool_class| words = Array(tool_class.trigger_words) next if words.empty? normalized = words.flat_map { |w| w.downcase.gsub(/[^a-z ]/, ' ').split }.uniq normalized.each { |word| add_tool_for_word(word, tool_class) } end end |
.clear ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/legion/tools/trigger_index.rb', line 67 def clear if defined?(Concurrent::Map) && @index.is_a?(Concurrent::Map) @index = Concurrent::Map.new else @mutex.synchronize { @index = {} } end end |
.empty? ⇒ Boolean
49 50 51 52 53 54 55 |
# File 'lib/legion/tools/trigger_index.rb', line 49 def empty? if defined?(Concurrent::Map) && @index.is_a?(Concurrent::Map) @index.each_pair.none? else @index.empty? end end |
.match(word_set) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/legion/tools/trigger_index.rb', line 33 def match(word_set) matched = Set.new per_word = {} word_set.each do |word| normalized = word.to_s.downcase.gsub(/[^a-z ]/, ' ').strip next if normalized.empty? tools = read_word(normalized) next unless tools per_word[normalized] = tools matched.merge(tools) end [matched, per_word] end |
.size ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/legion/tools/trigger_index.rb', line 57 def size if defined?(Concurrent::Map) && @index.is_a?(Concurrent::Map) count = 0 @index.each_pair { count += 1 } count else @index.size end end |