File: integrations.rb

package info (click to toggle)
ruby-rr 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,436 kB
  • ctags: 1,404
  • sloc: ruby: 11,657; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 1,122 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'set'

module RR
  module Integrations
  end

  def self.register_adapter(klass)
    adapter = Integrations::Decorator.new(klass.new)
    unless adapters_by_name.key?(adapter.name)
      adapters << adapter
      adapters_by_name[adapter.name] = adapter
    end
  end

  def self.adapters
    @adapters ||= []
  end

  def self.adapters_by_name
    @adapters_by_name ||= {}
  end

  def self.loaded_adapter_names
    adapters.
      select { |adapter| adapter.loaded? }.
      map { |adapter| adapter.name }
  end

  def self.applicable_adapters
    adapters.select { |adapter| adapter.applies? }
  end

  def self.find_applicable_adapter(name)
    adapters.
      select { |adapter| name === adapter.name.to_s }.
      find   { |adapter| adapter.applies? }
  end

  def self.module_shim_for_adapter(adapter)
    mod = Module.new
    (class << mod; self; end).class_eval do
      define_method(:included) do |base|
        # Note: This assumes that the thing that is including this module
        # is the same that the adapter detected and will hook into.
        adapter.load
      end
    end
    mod
  end
end