This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from zendesk/support_for_an_events_callback
Support for an events callback
- Loading branch information
Showing
8 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Kasket | ||
# Interface to the internal instrumentation event. | ||
module Events | ||
class << self | ||
# Invokes the configured events callback, if provided. | ||
# | ||
# The callback behaves like a listener, and receives the same arguments | ||
# that are passed to this `report` method. | ||
# | ||
# @param [String] event the type of event being instrumented. | ||
# @param [class] ar_klass the ActiveRecord::Base subclass that the event | ||
# refers to. | ||
# | ||
# @return [nil] | ||
# | ||
def report(event, ar_klass) | ||
return unless fn | ||
|
||
fn.call(event, ar_klass) | ||
nil | ||
end | ||
|
||
private | ||
|
||
def fn | ||
return @fn if defined?(@fn) | ||
|
||
@fn = Kasket::CONFIGURATION[:events_callback] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "helper" | ||
|
||
describe Kasket::Events do | ||
before do | ||
@previous = Kasket::CONFIGURATION[:events_callback] | ||
Kasket::Events.remove_instance_variable(:@fn) if Kasket::Events.instance_variable_defined?(:@fn) | ||
end | ||
|
||
after { Kasket::CONFIGURATION[:events_callback] = @previous } | ||
|
||
describe ".report" do | ||
describe "when there is no stats callback configured" do | ||
before do | ||
Kasket::CONFIGURATION[:events_callback] = nil | ||
end | ||
|
||
it "does nothing and returns safely" do | ||
assert_nil Kasket::Events.report("something", Author) | ||
end | ||
end | ||
|
||
describe "when a stats callback is configured" do | ||
before do | ||
@event = nil | ||
@ar_klass = nil | ||
|
||
callback = proc do |event, ar_klass| | ||
@event = event | ||
@ar_klass = ar_klass | ||
end | ||
|
||
Kasket::CONFIGURATION[:events_callback] = callback | ||
end | ||
|
||
it "returns safely" do | ||
assert_nil Kasket::Events.report("something", Author) | ||
end | ||
|
||
it "invokes the callback" do | ||
assert_nil @event | ||
assert_nil @ar_klass | ||
|
||
Kasket::Events.report("something", Author) | ||
|
||
assert_equal "something", @event | ||
assert_equal Author, @ar_klass | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters