Class: Whoosh::Plugins::Registry
- Inherits:
-
Object
- Object
- Whoosh::Plugins::Registry
- Defined in:
- lib/whoosh/plugins/registry.rb
Constant Summary collapse
- DEFAULT_GEMS =
Default mappings: gem name => accessor symbol
{ "ruby_llm" => :llm, "lingua-ruby" => :lingua, "keyword-ruby" => :keyword, "ner-ruby" => :ner, "loader-ruby" => :loader, "prompter-ruby" => :prompter, "chunker-ruby" => :chunker, "guardrails-ruby" => :guardrails, "rag-ruby" => :rag, "eval-ruby" => :eval_, "connector-ruby" => :connector, "sastrawi-ruby" => :sastrawi, "pattern-ruby" => :pattern, "onnx-ruby" => :onnx, "tokenizer-ruby" => :tokenizer, "zvec-ruby" => :zvec, "reranker-ruby" => :reranker, "sequel" => :db }.freeze
Instance Method Summary collapse
- #accessor_for(gem_name) ⇒ Object
- #config_for(accessor) ⇒ Object
- #configure(accessor, config) ⇒ Object
- #define_accessors(target) ⇒ Object
- #disable(accessor) ⇒ Object
- #disabled?(accessor) ⇒ Boolean
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #register(gem_name, accessor:, initializer: nil) ⇒ Object
- #registered?(gem_name) ⇒ Boolean
- #scan_gemfile_lock(content) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
30 31 32 33 34 35 36 37 |
# File 'lib/whoosh/plugins/registry.rb', line 30 def initialize @gems = {} @configs = {} @disabled = Set.new @mutex = Mutex.new register_defaults end |
Instance Method Details
#accessor_for(gem_name) ⇒ Object
47 48 49 |
# File 'lib/whoosh/plugins/registry.rb', line 47 def accessor_for(gem_name) @gems.dig(gem_name, :accessor) end |
#config_for(accessor) ⇒ Object
118 119 120 |
# File 'lib/whoosh/plugins/registry.rb', line 118 def config_for(accessor) @configs[accessor] end |
#configure(accessor, config) ⇒ Object
114 115 116 |
# File 'lib/whoosh/plugins/registry.rb', line 114 def configure(accessor, config) @configs[accessor] = config end |
#define_accessors(target) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/whoosh/plugins/registry.rb', line 76 def define_accessors(target) @gems.each do |gem_name, entry| accessor = entry[:accessor] next if @disabled.include?(accessor) initializer = entry[:initializer] config = @configs[accessor] || {} mutex = @mutex instance_var = :"@_plugin_#{accessor}" target.define_singleton_method(accessor) do cached = instance_variable_get(instance_var) return cached if cached mutex.synchronize do # Double-check inside lock cached = instance_variable_get(instance_var) return cached if cached instance = if initializer initializer.call(config) else begin require gem_name.tr("-", "/") rescue LoadError raise Whoosh::Errors::DependencyError, "Plugin '#{accessor}' requires gem '#{gem_name}' but it could not be loaded" end nil end instance_variable_set(instance_var, instance || true) instance end end end end |
#disable(accessor) ⇒ Object
122 123 124 |
# File 'lib/whoosh/plugins/registry.rb', line 122 def disable(accessor) @disabled.add(accessor) end |
#disabled?(accessor) ⇒ Boolean
126 127 128 |
# File 'lib/whoosh/plugins/registry.rb', line 126 def disabled?(accessor) @disabled.include?(accessor) end |
#register(gem_name, accessor:, initializer: nil) ⇒ Object
39 40 41 |
# File 'lib/whoosh/plugins/registry.rb', line 39 def register(gem_name, accessor:, initializer: nil) @gems[gem_name] = { accessor: accessor, initializer: initializer } end |
#registered?(gem_name) ⇒ Boolean
43 44 45 |
# File 'lib/whoosh/plugins/registry.rb', line 43 def registered?(gem_name) @gems.key?(gem_name) end |
#scan_gemfile_lock(content) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/whoosh/plugins/registry.rb', line 51 def scan_gemfile_lock(content) specs_section = false detected = [] content.each_line do |line| stripped = line.strip if stripped == "specs:" specs_section = true next end if specs_section break if stripped.empty? || !line.start_with?(" ") # Lines like " lingua-ruby (0.1.0)" match = stripped.match(/\A(\S+)\s+\(/) if match && @gems.key?(match[1]) detected << match[1] end end end detected end |