Sha256: a81d59b0c64a3877ccd52c1fbdd62e18ddfd066f488d830d86247fe71ae4832f

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

class Rack::Tracker::HandlerDelegator
  class << self
    def handler(method_name)
      new.handler(method_name)
    end
  end

  attr_accessor :env

  def initialize(env={})
    @env = env
  end

  def method_missing(method_name, *args, &block)
    if respond_to?(method_name)
      write_event(handler(method_name).track(method_name, *args, &block))
    else
      super
    end
  end

  def write_event(event)
    if env.key?('tracker')
      self.env['tracker'].deep_merge!(event) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
    else
      self.env['tracker'] = event
    end
  end

  def respond_to?(method_name, include_private=false)
    handler(method_name).respond_to?(:track, include_private)
  end

  def handler(method_name)
    return method_name if method_name.kind_of?(Class)

    _handler = method_name.to_s.camelize
    ["Rack::Tracker::#{_handler}", _handler].detect do |const|
      begin
        return const.constantize
      rescue NameError
        false
      end
    end

    raise ArgumentError, "No such Handler: #{_handler}"
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rack-tracker-0.3.0 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.2.6 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.2.5 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.2.4 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.2.3 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.2.2 lib/rack/tracker/handler_delegator.rb