Sha256: a99d92a10d27bdebad5357d3dfdbd33ebd351bca446654921a45893fd1795dee

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

class Chronometer
  class DSL
    attr_reader :events, :tracepoints

    def initialize
      @events = []
      @tracepoints = []
    end

    def for_class(cls)
      @cls && raise('already in for_class')
      @cls = cls

      yield
    ensure
      @cls = nil
    end

    def for_singleton_class(cls, &blk)
      for_class(cls.singleton_class, &blk)
    end

    def method(method_name, **opts)
      opts[:name] ||= @cls.singleton_class? ? "#{ObjectSpace.each_object(@cls).to_a.last}.#{method_name}" : "#{@cls}##{method_name}"
      opts.delete(:method) && raise('Cannot specify :method')
      opts[:method] = method_name
      opts[:event_type] ||= :X
      opts[:cls] = @cls || raise('must be in for_class block')
      events << Event.new(opts)
    end

    def methods(*method_names, **opts)
      method_names.flatten.each do |method_name|
        method method_name, **opts
      end
    end

    def tracepoint(event_name, &blk)
      @cls && raise('in for_class block')

      tracepoints << [event_name, blk]
    end
  end
  private_constant :DSL
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chronometer-0.2.0 lib/chronometer/dsl.rb
chronometer-0.1.4 lib/chronometer/dsl.rb
chronometer-0.1.3 lib/chronometer/dsl.rb
chronometer-0.1.1 lib/chronometer/dsl.rb
chronometer-0.1.0 /Users/segiddins/Development/OpenSource/chronometer/lib/chronometer/dsl.rb