Module: Tom::Routes

Defined in:
lib/tom/routes.rb

Overview

Takes care of registering adapters and mergers for routes, and then matching routes and methods to adapters and mergers.

Class Method Summary collapse

Class Method Details

.adaptersHash

Returns See what register_adapter returns.

Returns:



68
69
70
# File 'lib/tom/routes.rb', line 68

def self.adapters
  @adapters
end

.mergersHash

Returns See what register_merger returns.

Returns:



73
74
75
# File 'lib/tom/routes.rb', line 73

def self.mergers
  @mergers
end

.register(opts) ⇒ Hash

Registers a opts or opts for the given opts.

This method should not be called directly in your app, you would use Adapter.register_route or Merger.register_route in your subclass of Adapter or Merger

Parameters:

  • opts (Hash)

    Needs to have at least :route and either :adapter or :merger set, depending on what you are registering. But you don’t need to use this method directly, Adapter and Merger provide convenience methods for you.

Returns:



23
24
25
26
27
# File 'lib/tom/routes.rb', line 23

def self.register(opts)
  return register_adapter(opts) if opts[:adapter]
  return register_merger(opts)  if opts[:merger]
  raise "You need to supply opts[:adapter] or opts[:merger]"
end

.register_adapter(opts) ⇒ Hash

Registers an Adapter for a given route and request method

Parameters:

  • opts (Hash)

    Needs to have the :route and :adapter keys set with a regular expression and a subclass of Adapter

Returns:

  • (Hash)

    First level are the HTTP methods, second level are the regular expressions for routes, and the values are arrays of Adapters for the combination of request method and route.



39
40
41
42
43
44
45
46
47
48
# File 'lib/tom/routes.rb', line 39

def self.register_adapter(opts)
  validate_type(opts[:adapter], Adapter)
  methods = get_methods(opts)
  @adapters ||= default_methods_hash
  methods.each do |method|
    @adapters[method][opts[:route]] ||= []
    @adapters[method][opts[:route]] << opts[:adapter]
  end
  @adapters
end

.register_merger(opts) ⇒ Hash

Registers a Merger for a given route and request method

Returns:

  • (Hash)

    First level are the HTTP methods, second level are the regular expressions for routes, and the values are arrays of Mergers for the combination of request method and route.



56
57
58
59
60
61
62
63
64
65
# File 'lib/tom/routes.rb', line 56

def self.register_merger(opts)
  validate_type(opts[:merger], Merger)
  methods = get_methods(opts)
  @mergers ||= default_methods_hash
  methods.each do |method|
    @mergers[method][opts[:route]] ||= []
    @mergers[method][opts[:route]] << opts[:merger]
  end
  @mergers
end