Sha256: d49ca8f6032b37f513776d3c7d83f0f9b69177440a9f97f752aeca10dae5d305

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'vizsla/helpers'

module Vizsla
  ##
  # 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:
  #
  #   ::Vizsla::Patches.patch_foo
  #
  class Patches
    include ::Vizsla::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 "vizsla/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

1 entries across 1 versions & 1 rubygems

Version Path
tracebin-0.0.7 lib/vizsla/patches.rb