Sha256: 1f522d37021d2e58efbd782cd72214783479094df8f3508b5acb3a3080238f1c

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require 'tracebin/helpers'

module Tracebin
  ##
  # This singleton class handles patching for any given library we wish to
  # instrument. To create a new patch for a library, just create a file in the
  # +lib/patches+ directory with any name. These files typically contain code
  # that will monkeypatch a given library. When you wish to execute the code
  # in that file, just call its corresponding +patch_+ method. For example, if
  # we have a file +lib/patches/foo.rb+, then we would just call:
  #
  #   ::Tracebin::Patches.patch_foo
  #
  class Patches
    include ::Tracebin::Helpers

    PATCH_METHOD_REGEX = /^patch_(.*)$/

    class << self
      def handle_event(handler_name, event_data)
        handler = instance_variable_get "@#{handler_name}_event_handler"
        handler.call event_data unless handler.nil?
      end

      def method_missing(method_sym, *args, &block)
        if method_sym.to_s =~ PATCH_METHOD_REGEX
          patch_name = $1
          instance_variable_set "@#{patch_name}_event_handler", block
          require "tracebin/patches/#{patch_name}"
        else
          super
        end
      end

      def respond_to?(method_sym, include_private = false)
        if method_sym.to_s =~ PATCH_METHOD_REGEX
          true
        else
          super
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tracebin-0.0.13 lib/tracebin/patches.rb
tracebin-0.0.12 lib/tracebin/patches.rb
tracebin-0.0.11 lib/tracebin/patches.rb
tracebin-0.0.10 lib/tracebin/patches.rb
tracebin-0.0.9 lib/tracebin/patches.rb
tracebin-0.0.8 lib/tracebin/patches.rb