# File lib/fluent/registry.rb, line 23 def initialize(kind, search_prefix, dir_search_prefix: nil) @kind = kind @search_prefix = search_prefix @dir_search_prefix = dir_search_prefix @map = {} @paths = [DEFAULT_PLUGIN_PATH] end
# File lib/fluent/registry.rb, line 38 def lookup(type) type = type.to_sym if value = @map[type] return value end search(type) if value = @map[type] return value end raise ConfigError, "Unknown #{@kind} plugin '#{type}'. Run 'gem search -rd fluent-plugin' to find plugins" # TODO error class end
# File lib/fluent/registry.rb, line 33 def register(type, value) type = type.to_sym @map[type] = value end
# File lib/fluent/registry.rb, line 50 def reverse_lookup(value) @map.each do |k, v| return k if v == value end nil end
# File lib/fluent/registry.rb, line 57 def search(type) # search from additional plugin directories if @dir_search_prefix path = "#{@dir_search_prefix}#{type}" files = @paths.map { |lp| lpath = File.expand_path(File.join(lp, "#{path}.rb")) File.exist?(lpath) ? lpath : nil }.compact unless files.empty? # prefer newer version require files.sort.last return end end path = "#{@search_prefix}#{type}" # prefer LOAD_PATH than gems files = $LOAD_PATH.map { |lp| lpath = File.expand_path(File.join(lp, "#{path}.rb")) File.exist?(lpath) ? lpath : nil }.compact unless files.empty? # prefer newer version require files.sort.last return end specs = Gem::Specification.find_all { |spec| spec.contains_requirable_file? path } # prefer newer version specs = specs.sort_by { |spec| spec.version } if spec = specs.last spec.require_paths.each { |lib| file = "#{spec.full_gem_path}/#{lib}/#{path}" require file } end end