Class: Fluent::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/registry.rb

Constant Summary collapse

DEFAULT_PLUGIN_PATH =
File.expand_path('../plugin', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, search_prefix, dir_search_prefix: nil) ⇒ Registry

Returns a new instance of Registry.



23
24
25
26
27
28
29
# 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

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



31
32
33
# File 'lib/fluent/registry.rb', line 31

def kind
  @kind
end

#pathsObject (readonly)

Returns the value of attribute paths.



31
32
33
# File 'lib/fluent/registry.rb', line 31

def paths
  @paths
end

Instance Method Details

#lookup(type) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
# 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

#register(type, value) ⇒ Object



33
34
35
36
# File 'lib/fluent/registry.rb', line 33

def register(type, value)
  type = type.to_sym
  @map[type] = value
end

#reverse_lookup(value) ⇒ Object



50
51
52
53
54
55
# File 'lib/fluent/registry.rb', line 50

def reverse_lookup(value)
  @map.each do |k, v|
    return k if v == value
  end
  nil
end

#search(type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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