File: traceable.rb

package info (click to toggle)
ruby-naught 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: ruby: 658; makefile: 6
file content (29 lines) | stat: -rw-r--r-- 807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require "naught/null_class_builder/command"

module Naught
  class NullClassBuilder
    module Commands
      # Records the source location where a null object was created
      #
      # @api private
      class Traceable < Command
        # Install the traceable initializer
        # @return [void]
        # @api private
        def call
          defer_prepend_module do
            attr_reader :__file__, :__line__

            define_method(:initialize) do |options = {}|
              backtrace = options.fetch(:caller) { Kernel.caller(3) }
              caller_data = Naught::CallerInfo.parse(backtrace[0])
              @__file__ = caller_data[:path]
              @__line__ = caller_data[:lineno]
              super(options)
            end
          end
        end
      end
    end
  end
end