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.



77
78
79
80
# File 'lib/rack/tracker.rb', line 77

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

Instance Method Details

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



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

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


90
91
92
93
94
95
# File 'lib/rack/tracker.rb', line 90

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