Sha256: c6a79dcee9eea5ef4e1ecc81918f0ba36bb84f2e67f8549259ae3bc0d153214e

Contents?: true

Size: 1.86 KB

Versions: 20

Compression:

Stored size: 1.86 KB

Contents

require 'skylight/normalizers/default'

module Skylight
  # Convert AS::N events to Skylight events
  module Normalizers

    DEFAULT = Default.new

    def self.register(name, klass)
      (@registry ||= {})[name] = klass
      klass
    end

    def self.build(config)
      normalizers = {}

      (@registry || {}).each do |k, klass|
        unless klass.method_defined?(:normalize)
          # TODO: Warn
          next
        end

        normalizers[k] = klass.new(config)
      end

      Container.new(normalizers)
    end

    class Normalizer
      def self.register(name)
        Normalizers.register(name, self)
      end

      attr_reader :config

      def initialize(config)
        @config = config
        setup if respond_to?(:setup)
      end
    end

    class RenderNormalizer < Normalizer
      def setup
        @paths = config['normalizers.render.view_paths'] || []
      end

      def normalize_render(category, payload, annotations)
        if path = payload[:identifier]
          title = relative_path(path)
          path = nil if path == title
        end

        [ category, title, path, annotations ]
      end

      def relative_path(path)
        root = @paths.find { |p| path.start_with?(p) }

        if root
          relative = path[root.size..-1]
          relative = relative[1..-1] if relative.start_with?("/")
          relative
        else
          path
        end
      end
    end

    class Container
      def initialize(normalizers)
        @normalizers = normalizers
      end

      def normalize(trace, name, payload)
        normalizer = @normalizers[name] || DEFAULT
        normalizer.normalize(trace, name, payload)
      end
    end

    %w( process_action
        render_collection
        render_partial
        render_template
        send_file
        sql).each do |file|
      require "skylight/normalizers/#{file}"
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
skylight-0.2.0.beta.1 lib/skylight/normalizers.rb
skylight-0.1.8 lib/skylight/normalizers.rb
skylight-0.1.7 lib/skylight/normalizers.rb
skylight-0.1.7.alpha1 lib/skylight/normalizers.rb
skylight-0.1.6 lib/skylight/normalizers.rb
skylight-0.1.6.alpha3 lib/skylight/normalizers.rb
skylight-0.1.6.alpha1 lib/skylight/normalizers.rb
skylight-0.1.5 lib/skylight/normalizers.rb
skylight-0.1.5.alpha2 lib/skylight/normalizers.rb
skylight-0.1.5.alpha1 lib/skylight/normalizers.rb
skylight-0.1.4 lib/skylight/normalizers.rb
skylight-0.1.4.alpha3 lib/skylight/normalizers.rb
skylight-0.1.4.alpha2 lib/skylight/normalizers.rb
skylight-0.1.4.alpha1 lib/skylight/normalizers.rb
skylight-0.1.3 lib/skylight/normalizers.rb
skylight-0.1.2 lib/skylight/normalizers.rb
skylight-0.1.1 lib/skylight/normalizers.rb
skylight-0.1.0 lib/skylight/normalizers.rb
skylight-0.1.0.alpha2 lib/skylight/normalizers.rb
skylight-0.1.0.alpha1 lib/skylight/normalizers.rb