Class: Rack::Tracker::HandlerSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/tracker.rb

Defined Under Namespace

Classes: Handler

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ HandlerSet

Returns a new instance of HandlerSet.



82
83
84
85
# File 'lib/rack/tracker.rb', line 82

def initialize(&block)
  @handlers = []
  instance_exec(&block) if block_given?
end

Instance Method Details

#each(env = {}, &block) ⇒ Object



102
103
104
# File 'lib/rack/tracker.rb', line 102

def each(env = {}, &block)
  @handlers.map { |h| h.init(env) }.each(&block)
end

#handler(name, configuration = {}, &block) ⇒ Object

setup the handler class with configuration options and make it ready for receiving the env during injection

usage:

use Rack::Tracker do
  handler :google_analytics, { tracker: 'U-XXXXX-Y' }
end


95
96
97
98
99
100
# File 'lib/rack/tracker.rb', line 95

def handler(name, configuration = {}, &block)
  # we need here "something" (which is atm the handler struct)
  # to postpone the initialization of the handler,
  # to give it the env and configuration options when the result of the handler is injected into the response.
  @handlers << Handler.new(Rack::Tracker::HandlerDelegator.handler(name), configuration)
end