Sha256: 82f4decec52303665b827c11561c38c26faec39d149f453443f898199f32ccf7

Contents?: true

Size: 1.2 KB

Versions: 25

Compression:

Stored size: 1.2 KB

Contents

require 'active_support'

module ActiveSpy
  # Module that defines methods used to spy on some class methods
  #
  module Spy
    # Default snippet to extends the class with {ActiveSpy::Spy::ClassMethods}
    # when {ActiveSpy::Spy} is included in it.
    #
    def self.included(base)
      base.extend ClassMethods
    end

    # Invokes the callback method on the invoker class. The +callback_type+
    # param tells wether it will be called +:after+ or +before+.
    #
    def invoke_callback(method, callback_type)
      callback_invoker = callback_invoker_class.new(self)
      callback = "#{callback_type}_#{method}"
      return unless callback_invoker.respond_to?(callback)
      callback_invoker.send(callback)
    end

    # Gets the invoker class based on the class' name
    #
    def callback_invoker_class
      ActiveSupport::Inflector.constantize "#{self.class.name}Events"
    end

    # Class methods to be defined in classes that includes {ActiveSpy::Spy}
    #
    module ClassMethods
      # Set watchers for the +method+
      #
      def watch_method(*methods)
        methods.each do |method|
          ActiveSpy::SpyList << { 'class' => name, 'method' => method }
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
active_spy-1.4.4 lib/active_spy/spy/spy.rb
active_spy-1.4.3 lib/active_spy/spy/spy.rb
active_spy-1.4.2 lib/active_spy/spy/spy.rb
active_spy-1.4.1 lib/active_spy/spy/spy.rb
active_spy-1.4.0 lib/active_spy/spy/spy.rb
active_spy-1.3.12 lib/active_spy/spy/spy.rb
active_spy-1.3.11 lib/active_spy/spy/spy.rb
active_spy-1.3.10 lib/active_spy/spy/spy.rb
active_spy-1.3.9 lib/active_spy/spy/spy.rb
active_spy-1.3.8 lib/active_spy/spy/spy.rb
active_spy-1.3.7 lib/active_spy/spy/spy.rb
active_spy-1.3.6 lib/active_spy/spy/spy.rb
active_spy-1.3.5 lib/active_spy/spy/spy.rb
active_spy-1.3.4 lib/active_spy/spy/spy.rb
active_spy-1.3.3 lib/active_spy/spy/spy.rb
active_spy-1.3.2 lib/active_spy/spy/spy.rb
active_spy-1.3.1 lib/active_spy/spy/spy.rb
active_spy-1.3.0 lib/active_spy/spy/spy.rb
active_spy-1.2.0 lib/active_spy/spy/spy.rb
active_spy-1.1.3 lib/active_spy/spy/spy.rb