Class: Taksi::Registry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/taksi/registry.rb

Defined Under Namespace

Classes: InterfaceAltenativeNotFoundError, InterfaceNotFoundError

Constant Summary collapse

NAME_REGEX =
%r{^[a-z\-_/]{1,80}$}i

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



18
19
20
# File 'lib/taksi/registry.rb', line 18

def initialize
  clear!
end

Instance Method Details

#add(klass, name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/taksi/registry.rb', line 22

def add(klass, name)
  unless name.to_s.match?(NAME_REGEX)
    raise StandardError,
          "Invalid interface name '#{name}', it must to match regex '#{NAME_REGEX.inspect}'"
  end

  sym_name = name.to_sym

  @interfaces[sym_name] ||= []
  @interfaces[sym_name] << klass
end

#clear!Object



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

def clear!
  @interfaces = {}
end

#find(name, version, alternative = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/taksi/registry.rb', line 38

def find(name, version, alternative = nil)
  interfaces_from_name = @interfaces[name.to_sym]

  raise InterfaceNotFoundError if interfaces_from_name.nil?

  parsed_version = ::Gem::Version.new(version)

  found_interface = interfaces_from_name.find do |interface|
    next false unless interface.version_pattern.satisfied_by?(parsed_version)

    next true if alternative.nil?

    next true if interface.alternatives.nil?

    next true if interface.alternatives.include?(alternative)

    false
  end

  raise InterfaceNotFoundError if found_interface.nil?

  found_interface
end