Module: HTTPX::Registry::ClassMethods

Defined in:
lib/httpx/registry.rb

Overview

Class Methods

Instance Method Summary collapse

Instance Method Details

#inherited(klass) ⇒ Object



49
50
51
52
# File 'lib/httpx/registry.rb', line 49

def inherited(klass)
  super
  klass.instance_variable_set(:@registry, @registry.dup)
end

#register(tag, handler) ⇒ Symbol, ...

Returns the handler (if Symbol or String, it is assumed to be an autoloaded module, to be loaded later).

Parameters:

  • tag (Object)

    the identifier for the handler in the registry

Returns:

  • (Symbol, String, Object)

    the handler (if Symbol or String, it is assumed to be an autoloaded module, to be loaded later)



77
78
79
# File 'lib/httpx/registry.rb', line 77

def register(tag, handler)
  registry[tag] = handler
end

#registry(tag = nil) ⇒ Symbol, ...

Returns the corresponding handler (if Symbol or String, will assume it referes to an autoloaded module, and will load-and-return it).

Parameters:

  • tag (Object) (defaults to: nil)

    the handler identifier in the registry

Returns:

  • (Symbol, String, Object)

    the corresponding handler (if Symbol or String, will assume it referes to an autoloaded module, and will load-and-return it).

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/httpx/registry.rb', line 58

def registry(tag = nil)
  @registry ||= {}
  return @registry if tag.nil?

  handler = @registry.fetch(tag)
  raise(Error, "#{tag} is not registered in #{self}") unless handler

  case handler
  when Symbol, String
    const_get(handler)
  else
    handler
  end
end