Sha256: 1c6175dd0a361df3a9726c7271821542ebc959485cc7fe2a0baeb112893f0ca2

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 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 = env['tracker'].deep_merge!(event)
    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

2 entries across 2 versions & 1 rubygems

Version Path
rack-tracker-0.0.3 lib/rack/tracker/handler_delegator.rb
rack-tracker-0.0.2 lib/rack/tracker/handler_delegator.rb