Sha256: e3dbf9bc6e266186956ad667acee9f89768f6f773e61c87c90ce61ef0bff14db

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

module Skylight
  module Helpers
    module ClassMethods
      def method_added(name)
        super

        if opts = @__sk_instrument_next_method
          @__sk_instrument_next_method = nil
          title = "#{to_s}##{name}"
          __sk_instrument_method_on(self, name, title, opts)
        end
      end

      def singleton_method_added(name)
        super

        if opts = @__sk_instrument_next_method
          @__sk_instrument_next_method = nil
          title = "#{to_s}.#{name}"
          __sk_instrument_method_on(__sk_singleton_class, name, title, opts)
        end
      end

      def instrument_method(*args)
        opts = args.pop if Hash === args.last

        if name = args.pop
          title = "#{to_s}##{name}"
          __sk_instrument_method_on(self, name, title, opts || {})
        else
          @__sk_instrument_next_method = opts || {}
        end
      end

    private

      def __sk_instrument_method_on(klass, name, title, opts)
        category = (opts[:category] || "app.method").to_s
        title    = (opts[:title] || title).to_s
        desc     = opts[:description].to_s if opts[:description]

        klass.class_eval <<-RUBY
          alias_method :"#{name}_before_instrument", :"#{name}"

          def #{name}(*args, &blk)
            span = Skylight.instrument(
              category:  :"#{category}",
              title:       #{title.inspect},
              description: #{desc.inspect})

            begin
              #{name}_before_instrument(*args, &blk)
            ensure
              span.done if span
            end
          end
        RUBY
      end

      if respond_to?(:singleton_class)
        alias :__sk_singleton_class :singleton_class
      else
        def __sk_singleton_class
          class << self; self; end
        end
      end
    end

    def self.included(base)
      base.class_eval do
        @__sk_instrument_next_method = nil
        extend ClassMethods
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
skylight-0.2.7 lib/skylight/helpers.rb
skylight-0.2.6 lib/skylight/helpers.rb
skylight-0.2.5 lib/skylight/helpers.rb
skylight-0.2.4 lib/skylight/helpers.rb
skylight-0.2.3 lib/skylight/helpers.rb
skylight-0.2.2 lib/skylight/helpers.rb
skylight-0.2.1 lib/skylight/helpers.rb
skylight-0.2.0 lib/skylight/helpers.rb
skylight-0.2.0.beta.4 lib/skylight/helpers.rb
skylight-0.2.0.beta.3 lib/skylight/helpers.rb
skylight-0.2.0.beta.2 lib/skylight/helpers.rb
skylight-0.2.0.beta.1 lib/skylight/helpers.rb
skylight-0.1.8 lib/skylight/helpers.rb
skylight-0.1.7 lib/skylight/helpers.rb