Class: Gem::MiniMirror::ResourceHandler

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubygems/mini_mirror/resource_handler.rb

Defined Under Namespace

Classes: AlreadyRegistered, HandlerNotFound

Instance Method Summary collapse

Constructor Details

#initializeResourceHandler

Returns a new instance of ResourceHandler.



28
29
30
31
# File 'lib/rubygems/mini_mirror/resource_handler.rb', line 28

def initialize
  @handlers = {}
  @handlers_by_file_extension = {}
end

Instance Method Details

#add(klass, type, exts = []) ⇒ Object

Raises:



33
34
35
36
37
38
39
# File 'lib/rubygems/mini_mirror/resource_handler.rb', line 33

def add(klass, type, exts = [])
  raise AlreadyRegistered, type if @handlers.key?(type.to_s)
  @handlers[type.to_s] = klass
  exts.each do |ext|
    @handlers_by_file_extension[ext.to_s] = klass unless ext.to_s.empty?
  end
end

#find(options = {}) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubygems/mini_mirror/resource_handler.rb', line 49

def find(options = {})
  unless options.key?(:ext)
    if path = options[:path]
      options[:ext] = File.extname(path)
    end
  end
  handler = find_handler_by_type(options[:type]) || find_handler_by_extension(options[:ext])
  raise HandlerNotFound, options unless handler
  handler
end

#find_handler_by_extension(ext) ⇒ Object



41
42
43
# File 'lib/rubygems/mini_mirror/resource_handler.rb', line 41

def find_handler_by_extension(ext)
  @handlers_by_file_extension[ext.to_s]
end

#find_handler_by_type(type) ⇒ Object



45
46
47
# File 'lib/rubygems/mini_mirror/resource_handler.rb', line 45

def find_handler_by_type(type)
  @handlers[type.to_s]
end